Sockets and threading

zayatzz alan.kesselmann at gmail.com
Sun Jul 12 15:16:29 EDT 2009


Im trying to get aquinted to python on bit more basic level and am
following socket and threading programming tutorials from these 2
addresses :

http://heather.cs.ucdavis.edu/~matloff/Python/PyNet.pdf
http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf

in this PyThreads file he sets up threaded server app which listens to
what clients send to it and echos it back to clients while adding sent
info into one string.

The problem is that the code does not work for me.

class srvr(threading.Thread):
	v = ""
	vlock = threading.Lock()
	id = 0
	def __init__(self,clntsock):
		threading.Thread.__init__(self)
		self.myid = srvr.id
		srvr.id += 1
		self.myclntsock = clntsock
	def run(self):
		while 1:
			k = self.myclntsock.recv(1)
			if k == "": break
			srvr.vlock.acquire()
			srvr.v += k
			srvr.vlock.release()
			self.myclntsock.send(srvr.v)
			self.myclntsock.close()

Instead of sendint back i get this error :
  File "server3.py", line 31, in serveclient
    k = self.myclntsock.recv(1)
  File "/usr/lib/python2.6/socket.py", line 165, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

As much as i understand this line 31 is supposed to recieve stuff from
clients and has buffer size 1. I dont understand what this has to do
with file descriptor.. whatever that is anyway.

Alan



More information about the Python-list mailing list