Handle one HTTP request from python with timeout - python

I would like to write an application where the main thread start an HTTP server what have to wait for exactly one HTTP request for at most 10 seconds. The application have to be blocked until the request is received (and processed) or the timeout is exceeded.
I tried to use this code:
import BaseHTTPServer
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write("<html><head><title>Title</title></head>")
httpd = BaseHTTPServer.HTTPServer(('', 8001), RequestHandler)
httpd.socket.settimeout(10)
httpd.handle_request()
The problem is that when the server receives a request, I get this error message:
Exception happened during processing of request from ('127.0.0.1', 51321)
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 639, in __init__
self.handle()
File "C:\Python27\lib\BaseHTTPServer.py", line 343, in handle
self.handle_one_request()
File "C:\Python27\lib\BaseHTTPServer.py", line 313, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "C:\Python27\lib\socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10035] A non-blocking socket operation could not be completed immediately
If I remove the settimeout function, than I don't get this error, but I lost the timeout also. I tried to use the setblocking function too, but it destroy the effect of the settimeout.
How can I reach my goal?
PS.: I am using Python 2.7.2 on a 64-bit Windows 7

Related

How to keep Werkzeug from crashing after NGINX closes pipe?

I have a Werkzeug server running behind NGINX. When a client disconnects while waiting for the Werkzeug server to respond, NGINX closes the pipe to Werkzeug. When the python program writes the response to Werkzeug, the following exception occurs and Werkzeug crashes:
Traceback (most recent call last):
File "server.py", line 81, in
app.run(host=args.host, port=args.port, debug=False)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 843, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 694, in run_simple
inner()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 659, in inner
srv.serve_forever()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 499, in serve_forever
HTTPServer.serve_forever(self)
File "/usr/lib/python2.7/SocketServer.py", line 238, in serve_forever
self._handle_request_noblock()
File "/usr/lib/python2.7/SocketServer.py", line 297, in _handle_request_noblock
self.handle_error(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 651, in init
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 279, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
socket.error: [Errno 32] Broken pipe
Is there some configuration option I'm missing to keep it from crashing? Normally all exceptions are caught and a 500 error returned, with the server remaining alive.
As far as I can guess from the debug trace and without reading the source code, you are probably closing the socket prematurely.
Your server process has received a SIGPIPE writing to a socket. This usually happens when you write to a socket fully closed on the other (client) side. This might be happening when a client program doesn't wait till all the data from the server is received and simply closes a socket (using close function).
In a C program you would normally try setting to ignore SIGPIPE signal or setting a dummy signal handler for it. In this case a simple error will be returned when writing to a closed socket. In your case a python seems to throw an exception that can be handled as a premature disconnect of the client.
How to prevent errno 32 broken pipe?

Exception happened during processing of request

Exception happened during processing of request from ('192.168.227.112', 49343)
Traceback (most recent call last):
File "/usr/lib/python2.6/SocketServer.py", line 283, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 309, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 322, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.6/SocketServer.py", line 617, in __init__
self.handle()
File "/usr/lib/python2.6/wsgiref/simple_server.py", line 130, in handle
self.raw_requestline = self.rfile.readline()
File "/usr/lib/python2.6/socket.py", line 444, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
The code block somewhere. Then I have to press Ctrl-c to kill the block point, so that the program can run continue. If the block happened, the program became very slow. Can anybody have good suggestions to solve this?
How about this?
you can put excepion when an error occurs.
for example script:
try:
...first_conditional
except KeyboardInterrupt:
...your_optional_conditional
Check this docs: https://docs.python.org/2/library/exceptions.html#exceptions.KeyboardInterrupt

Exception happened during processing of request from ('127.0.0.1', xxxx) in SocketServer

I wrote a preview function based on SimpleHTTPServer and SocketServer, I catch KeyboardInterrupt exception as I enter Ctrl-C to stop server:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import SimpleHTTPServer
import SocketServer
class Reuse_TCPServer(SocketServer.TCPServer):
timeout = 1
allow_reuse_address = True
def preview(port=8000):
try:
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = Reuse_TCPServer(("", port), Handler)
except OSError as e:
print("Could not listen on port {}".format(port))
sys.exit(getattr(e, 'exitcode', 1))
try:
httpd.serve_forever()
except (KeyboardInterrupt, SystemExit) as e:
print("Shutting down server")
httpd.socket.close()
if __name__ == "__main__":
preview()
But most of the time , if I open localhost:8000 and immediately(some seconds) enter 'Ctrl-C', it will display the message first and then close the socket:
127.0.0.1 - - [16/Apr/2014 22:20:42] code 404, message File not found
127.0.0.1 - - [16/Apr/2014 22:20:42] "GET /static/css/autumn.css HTTP/1.1" 404 -
^C----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52787)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 310, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
----------------------------------------
f^C----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52788)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 310, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
----------------------------------------
^CShutting down server
Does anyone know how to solve this problem?
I'm experiencing exactly the same. I found an old bug in Python 2.7 and a patch that was supposed to solve this. I checked it and the patch is of course already in SocketServer.py. However, it didn't solve this problem.
In any case, the explanation in the link I'm pasting here is worth to be read to try to get a hint.
https://bugs.python.org/issue14574

Python Bottle framework becoming non-responsive

I am having some problem with using python's Bottle framework(http://bottlepy.org/docs/dev/index.html) to host a webpage. It seems to work fine for certain period of time but now and then I get the following error and it fails to show the webpage. The script doesn't crash but the webpage becomes non responsive.
Any suggestions?
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 279, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
I also see the following error. But I'm guessing these occur if a request to a non-existent webpage/object is requested-
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "/usr/lib/python2.7/socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 104] Connection reset by peer
This question seems to be similar to How to prevent errno 32 broken pipe?
You received a SIGPIPE and this could be due to attempting to write to a closed socket. You could try to handle the exception with something like that:
except socket.error, e:
if isinstance(e.args, tuple):
print "Errno: %d" % e[0]
if e[0] == errno.EPIPE:
# Caught a peer disconnection
print "Remote host disconnected"

Broken pipe error in Django Nonrel when loading localhost

Running Django Nonrel, with Google App Engine 2.6.0 and Python 2.7, I'm getting this exception when trying to load for the first time localhost and localhost/admin (I expect it will happen with any page, though):
Exception happened during processing of request from ('127.0.0.1', 57011)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/google_appengine/google/appengine/tools/dev_appserver.py", line 2438, in __init__
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 641, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 694, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
Strangely enough, it only appears using Google Chrome. When using Firefox, it doesn't print any exception (or at least, I haven't been able to replicate this problem in Firefox after many tries).
Does anyone know something about this problem?.
Thanks
There have been a few similar reports of race condition issues between Chrome and dev_appserver.py. The usual story is that Chrome opens multiple concurrent connections to the server, but sends a request on the second connection first. Because dev_appserver is single-threaded, the first request blocks, and the server hangs until someone gives up.
Supposedly starting Chrome with --disable-preconnect prevents this condition.

Categories