Select weirdness
Ron Garret
rNOSPAMon at flownet.com
Sun Apr 22 14:03:29 EDT 2007
In article <rNOSPAMon-36DDEF.23212021042007 at news.gha.chartermi.net>,
Ron Garret <rNOSPAMon at flownet.com> wrote:
> 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.
I have reproduced the problem using Telnet, so that proves it's not an
EOL issue. The problem seems to be timing-related. If I type the
request in manually it works. If I paste it in all at once, it hangs.
It's actually even weirder than that: if I pipe the request into a
telnet client then it hangs after the request line, just with a browser
(or wget). But if I literally paste the request into a window running a
telnet client, it gets past the request line and four out of seven
headers before it hangs.
rg
More information about the Python-list
mailing list