Select weirdness

Ron Garret rNOSPAMon at flownet.com
Sun Apr 22 02:21:21 EDT 2007


Here's my code.  It's a teeny weeny little HTTP server.  (I'm not really 
trying to reinvent the wheel here.  What I'm really doing is writing a 
dispatching proxy server, but this is the shortest way to illustrate the 
problem I'm having):

from SocketServer import *
from socket import *
from select import select

class myHandler(StreamRequestHandler):
  def handle(self):
    print '>>>>>>>>>>>'
    while 1:
      sl = select([self.rfile],[],[])[0]
      if sl:
        l = self.rfile.readline()
        if len(l)<3: break
        print l,
        pass
      pass
    print>>self.wfile, 'HTTP/1.0 200 OK'
    print>>self.wfile, 'Content-type: text/plain'
    print>>self.wfile
    print>>self.wfile, 'foo'
    self.rfile.close()
    self.wfile.close()
    print '<<<<<<<<<<<<'
    pass
  pass

def main():
  server = TCPServer(('',8080), myHandler)
  server.serve_forever()
  pass

if __name__ == '__main__': main()


If I telnet into this server and type in an HTTP request manually it 
works fine.  But when I try to access this with a browser (I've tried 
Firefox and Safari -- both do the same thing) it hangs immediately after 
reading the first line in the request (i.e. before reading the first 
header).

When I click the "stop" button in the browser it breaks the logjam and 
the server reads the headers (but then of course it dies trying to write 
the response to a now-closed socket).

The only difference I can discern is that the browser send \r\n for 
end-of-line while telnet just sends \n.  But I don't see why that should 
make any difference.  So I'm stumped.  Any clues would be much 
appreciated.

Thanks,
rg



More information about the Python-list mailing list