socket.makefile() question

Charles Follett cfollett at gac.edu
Mon Dec 13 22:15:59 EST 1999


It is my understanding from the documentation that socket.makefile()
takes a socket object and returns a nice file object, with all the
associated methods. I attempt to use this in the attached code, but to
no avail; I get an error every time I try and write to it:

Traceback (innermost last):
  File "/var/tmp/python-root/usr/lib/python1.5/threading.py", line 376, in __bootstrap
    self.run()
  File "/var/tmp/python-root/usr/lib/python1.5/threading.py", line 364, in run
    apply(self.__target, self.__args, self.__kwargs)
  File "./server.py", line 31, in service
    wf.flush()
IOError: [Errno 32] Broken pipe

The client (telnet) never recieves the message. Here is the
code. Perhaps someone can explain what I am doing wrong.

Thanks..
charley

#!/usr/bin/python
import sys
from socket import *
from threading import *

HOST = ''             #localhost
PORT = 5555

class server:
    def __init__(self):
        self.lastmsg = 'generic message\n'
        self.msgsp = Semaphore()
        s = socket(AF_INET, SOCK_STREAM)
        s.bind(HOST, PORT)
        s.listen(5)           #buffer up to 5 connections
        print 'server bound and listening..'
        self.loop(s)

    def loop(self, s):
        conn, addr = s.accept()
        print addr, 'connected..'
        client_thread = Thread(None,self.service,None,(conn,s),{})
        client_thread.start()
        print 'starting', client_thread.getName()
        self.loop(s)

    def service(self, conn, s):
        wf = s.makefile('wb')
        rf = s.makefile('rb')
        wf.write(self.lastmsg)
        wf.flush()
        data = rf.read()
        self.msgsp.acquire()
        self.lastmsg = data
        self.msgsp.release()
        conn.close()

if __name__=='__main__':
    try:
        s = server()
    except KeyboardInterrupt:
        sys.exit()





More information about the Python-list mailing list