connection to server not accepted (but no error) only after several hours

seb sebastien.thur at laposte.net
Tue Jan 16 10:39:35 EST 2007


Hi, this simple server (time protocol) does not respond after a few
hours, even when it is restarted. The behaviour looks to me like a
firewall blocking but I have desabled the firewall.
Using Netstat - a I find the server listed when it is running and not
listed when I stop it.
The server starts whith no error. But after about 6-8 hours there is no
way to make it respond again (even when it is restarted).

Once it is blocked, I killes it, wait for about 15 minutes without
running the server and then started again but this gives me the same
behaviour (no response).

The solution at this time is to reboot windows !!!

I have also tried to run it twice in order to get the exception that
the port is already used and an exeption is raised.
I have tried from another computer to connect to mine but it does not
manage.
  When I change the port in the server it responds immediatly.

I am running winXP SP2, python 2.4.

Do you have any clues ?
Thanks in advance.
Seb.

PS : I am calling this server from another program and make it run in a
thread.
Below is the standalone version (wich is adpated from effbot site).

import socket
import struct, time
import threading
import os
import appel_log2 as appel_log
import sys


class TimeServer(threading.Thread) :
	def __init__(self):
		nom_function_actuelle= str(sys._getframe().f_code.co_filename)
+"___"+str(sys._getframe().f_code.co_name)
		try :
			threading.Thread.__init__(self)
			self.log_file="timeserverlog.txt"
			self.PORT=37
			self.TIME1970=2208988800L
			self._continue=1
			self.time_shift=0
			message=nom_function_actuelle+"\t "+"STARTED OK "
			appel_log.write_log("info",message)
		except Exception, e :
			message=nom_function_actuelle+"\t "+"PB:::"+str(e)
			print message
			appel_log.write_log("warning",message)

	def set_log_file(self, file):
		nom_function_actuelle= str(sys._getframe().f_code.co_filename)
+"___"+str(sys._getframe().f_code.co_name)
		if os.path.exists(file):
			pass
		else :
			f=open(file,"w")
			f.close()
		self.log_file=file
		print "log file ",self.log_file
		self.log_file=file

	def reset_log_file(self):
		nom_function_actuelle= str(sys._getframe().f_code.co_filename)
+"___"+str(sys._getframe().f_code.co_name)
		print "resetting log file "
		if os.path.exists(self.log_file):
			f=open(self.log_file,"w")
			f.close()

	def set_time_shift(self,time_shift):
		self.time_shift=time_shift

	def run(self):
		nom_function_actuelle= str(sys._getframe().f_code.co_filename)
+"___"+str(sys._getframe().f_code.co_name)
		socket.timeout(1)
		service=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		service.bind(("", self.PORT))
		service.listen(1)
		print "listening on port", self.PORT
		while self._continue==1 :
    			channel, info = service.accept()
    			print "connection from", info
			message=str(time.time())+"\t"+str(time.asctime())+"\t"+str(info)+"\n"
			g=open(self.log_file,"a")
			g.write(message)
			g.close()
    			t = int(time.time()) + self.TIME1970 + self.time_shift
			t = struct.pack("!I", t)
    			channel.send(t) # send timestamp
   			channel.close() # disco
			m=nom_function_actuelle+" response OK "+str(info)
			appel_log.write_log("info",m)
			#print "apres m "
		print "time server self_continue=0"
		appel_log.write_log("warning",nom_function_actuelle+"\t
self._continue ="+str(self._continue))
		print "sortie du thread"

def main() :
	a=TimeServer()
	a.start()
	a.set_log_file("log_nw.txt")
	a.reset_log_file()
	while 1==1 :
		time.sleep(10)
		#a._continue=0
		pass
	time.sleep(2)

main()




More information about the Python-list mailing list