Error when serving html with WSGI - python

I am trying to make an application that serves a simple HTML form to the user and then calls a function when the user submits the form. It uses wsgiref.simple_server to serve the HTML. The server is encountering an error and I can't understand why. The code is as follows:
#!/usr/bin/python3
from wsgiref.simple_server import make_server
from wsgiref.util import setup_testing_defaults
import webbrowser # open user's web browser to url when server is run
from sys import exc_info
from traceback import format_tb
# Easily serves an html form at path_to_index with style at path_to_style
# Calls on_submit when the form is submitted, passing a dictionary with key
# value pairs { "input name" : submitted_value }
class SimpleServer:
def __init__(self, port=8000, on_submit=None, index_path="./index.html", css_path="./style.css"):
self.port = port
self.on_submit = on_submit
self.index_path = index_path
self.css_path = css_path
# Forwards request to proper method, or returns 404 page
def wsgi_app(self, environ, start_response):
urls = [
(r"^$", self.index),
(r"404$", self.error_404),
(r"style.css$", self.css)
]
path = environ.get("PATH_INFO", "").lstrip("/")
# Call another application if they called a path defined in urls
for regex, application in urls:
match = re.search(regex, path)
# if the match was found, return that page
if match:
environ["myapp.url_args"] = match.groups()
return application(environ, start_response)
return error_404(environ, start_response)
# Gives the user a form to submit all their input. If the form has been
# submitted, it sends the ouput of self.on_submit(user_input)
def index(self, environ, start_response):
# user_input is a dictionary, with keys from the names of the fields
user_input = parse_qs(environ['QUERY_STRING'])
# return either the form or the calculations
index_html = open(self.index_path).read()
body = index_html if user_input == {} else calculate(user_input)
mime_type = "text/html" if user_input == {} else "text/plain"
# return the body of the message
status = "200 OK"
headers = [ ("Content-Type", mime_type),
("Content-Length", str(len(body))) ]
start_response(status, headers)
return [body.encode("utf-8")]
def start_form(self):
httpd = make_server('', self.port, ExceptionMiddleware(self.wsgi_app))
url = "http://localhost:" + str(self.port)
print("Visit " + url)
# webbrowser.open(url)
httpd.serve_forever()
if __name__ == "__main__":
server = SimpleServer()
server.start_form()
When I run it, I get the error
127.0.0.1 - - [16/Dec/2014 21:15:57] "GET / HTTP/1.1" 500 0
Traceback (most recent call last):
File "/usr/lib/python3.4/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.4/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [16/Dec/2014 21:15:57] "GET / HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49354)
Traceback (most recent call last):
File "/usr/lib/python3.4/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.4/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.4/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/usr/lib/python3.4/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/socketserver.py", line 305, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.4/socketserver.py", line 331, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.4/socketserver.py", line 344, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.4/socketserver.py", line 669, in __init__
self.handle()
File "/usr/lib/python3.4/wsgiref/simple_server.py", line 133, in handle
handler.run(self.server.get_app())
File "/usr/lib/python3.4/wsgiref/handlers.py", line 144, in run
self.close()
File "/usr/lib/python3.4/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
This output doesn't actually include the script I am running, which I am confused about. Any thoughts?

Just to register the solution for this issue, the problem is with len() function.
str(len(body))
It calculate the wrong size and when return the server Content-Length, then it wait more bytes that needed.
Thus, always send bytes using a buffer with UTF-8, follow example:
from io import StringIO
stdout = StringIO()
print("Hello world!", file=stdout)
start_response("200 OK", [('Content-Type', 'text/plain; charset=utf-8')])
return [stdout.getvalue().encode("utf-8")]

Looking at your code I don't see a direct reason for this error. However, I would strongly advise that unless you're trying to learn how wsgi works (or implement your own framework), you should use an existing micro-framework. WSGI is NOT meant to be used directly by applications. It provides a very thin interface between Python and a web server.
A nice and light framework is bottle.py -- I use it for all Python webapps. But there are many many others, look for "Non Full-Stack Frameworks" in https://wiki.python.org/moin/WebFrameworks.
A nice advantage of bottle is that it's a single file, which makes it easy to distribute with your server.

Related

Internal Server Error, rather than raised AuthError response from Auth0

I have my flask-restful app.py which contains all of my main functions. I have created a server.py file as instructed from here: https://auth0.com/docs/quickstart/backend/python
In my app.py file from server i import AuthError and requires_auth. I have then put #requires_auth in front of my functions.
When I have a valid jwt, it works perfectly. When the jwt is not valid it fails. Failing is good, because the requests shouldn't work. But the response i get from my api is "Internal Server Error" rather than the detailed response in the raise AuthError section in the server.py file.
I get 2 errors:
Traceback (most recent call last):
File "C:\Users\ME\code\server.py", line 88, in decorated
issuer="https://"+AUTH0_DOMAIN+"/"
File "C:\Users\ME\lib\site-packages\jose\jwt.py", line 150, in decode
options=defaults)
File "C:\Users\ME\lib\site-packages\jose\jwt.py", line 457, in _validate_claims
_validate_exp(claims, leeway=leeway)
File "C:\Users\ME\lib\site-packages\jose\jwt.py", line 299, in _validate_exp
raise ExpiredSignatureError('Signature has expired.')
jose.exceptions.ExpiredSignatureError: Signature has expired.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ME\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\ME\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\ME\lib\site-packages\flask_restful\__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "C:\Users\ME\lib\site-packages\flask\views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "C:\Users\ME\lib\site-packages\flask_restful\__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "C:\Users\ME\code\server.py", line 92, in decorated
"description": "token is expired"}, 401)
server.AuthError: ({'code': 'token_expired', 'description': 'token is expired'}, 401)
How do i get the AuthError as the response to the call, rather than just my Internal Server Error?
Thanks!
There is an issue with this specific tutorial in Auth0, it instructs you to include the error handler in auth.py:
#app.errorhandler(AuthError)
def handle_auth_error(ex):
response = jsonify(ex.error)
response.status_code = ex.status_code
return response
Instead, you have to include this handler in your app.py, where you actually use #requires_auth.
Notice that to do so, you need to add relevant imports:
from auth import AuthError
from flask import jsonify
Notice: To be able to import from auth.py you need to add an empty file __init__.py in the same directory.
Try setting app.config[“PROPAGATE_EXCEPTIONS”] = True
Uou could maybe use an errorhandler to explicitly catch those errors and return some explicit json based on them.

Bottle GET request is broken with certain strings when running with paste server

I have a problem similar to this one:
Python bottle: UTF8 path string invalid when using app.mount()
When I try to GET /languages/Inglês I receive the error below (notice the accent "ê"). When passing [az] strings it works fine.
Critical error while processing request: /languages/Inglês
I tried the fix mentioned on the link above without success.
Working example:
from bottle import route, run, debug
#route('/languages/<name>')
def hello(name):
return name
if __name__ == '__main__':
debug(False)
#run(reloader=False, port = 8080) # works
run(server='paste', port = 8080) # fails
Running with server='paste' causes the crash, but using the Bottle server it runs OK. The problem seems to happen at the bottle._handle() method, where a UnicodeError is thrown (bottle.py line 844):
def _handle(self, environ):
path = environ['bottle.raw_path'] = environ['PATH_INFO']
if py3k:
try:
environ['PATH_INFO'] = path.encode('latin1').decode('utf8')
except UnicodeError:
return HTTPError(400, 'Invalid path string. Expected UTF-8')
I'm using Python 3.6.2, Bottle v0.12.13 and Paste 2.0.3 on a Windows 10 machine. What's going on? Is that a problem with Bottle or Paste?
Note: I've already solved my problem by refactoring all the code to use integer IDs instead of names. But I still would like to learn more about this.
Stack trace:
Critical error while processing request: /hello/inglês
Error:
RuntimeError('Request context not initialized.',)
Traceback:
Traceback (most recent call last):
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1661, in fget
try: return ls.var
AttributeError: '_thread._local' object has no attribute 'var'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 954, in wsgi
out = self._cast(self._handle(environ))
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 907, in _cast
out = self.error_handler.get(out.status_code, self.default_error_handler)(out)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 842, in default_error_handler
return tob(template(ERROR_PAGE_TEMPLATE, e=res))
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3619, in template
return TEMPLATES[tplid].render(kwargs)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3409, in render
self.execute(stdout, env)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3396, in execute
eval(self.co, env)
File "<string>", line 17, in <module>
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1249, in url
return self.urlparts.geturl()
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 165, in __get__
key, storage = self.key, getattr(obj, self.attr)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1663, in fget
raise RuntimeError("Request context not initialized.")
RuntimeError: Request context not initialized.
Answering my own question, quoting #GrahamDumpleton:
The Paste server is not tolerant of being sent non ASCII characters.
this is the bottle's problem!
In order to use paste server u can change this:
"""bottle.py"""
environ['PATH_INFO'] = path.encode('latin1').decode('utf8', 'ignore')
to:
environ['PATH_INFO'] = path.encode('utf8').decode('utf8', 'ignore') #utf-8 or else
it's work well.

Can't initiate get request in web.py

I'm trying to run two servers using web.py and initiating calls from one to another. Both servers start normally but when I try to call a url the below stack trace is thrown.
import web
urls = (
'/ping', 'Ping',
'/acqlock/+(.*)', 'Acquire',
)
class MSA(web.application):
def run(self, port=8081, *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, ('127.0.0.1', port))
app = MSA(urls, globals())
if __name__ == "__main__":
app.run(port=8081)
class Acquire:
def GET(self, resource_name):
print resource_name
response = app.request('http://127.0.0.1:8080/acqlock/' + resource_name, method='GET')
return response
But I keep getting this error after calling the /acqlock.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\web\wsgiserver\__init__.py", line 1245, in communicate
req.respond()
File "C:\Python27\lib\site-packages\web\wsgiserver\__init__.py", line 775, in respond
self.server.gateway(self).respond()
File "C:\Python27\lib\site-packages\web\wsgiserver\__init__.py", line 2018, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "C:\Python27\lib\site-packages\web\httpserver.py", line 306, in __call__
return self.app(environ, xstart_response)
File "C:\Python27\lib\site-packages\web\httpserver.py", line 274, in __call__
return self.app(environ, start_response)
File "C:\Python27\lib\site-packages\web\application.py", line 279, in wsgi
result = self.handle_with_processors()
File "C:\Python27\lib\site-packages\web\application.py", line 249, in handle_with_processors
return process(self.processors)
File "C:\Python27\lib\site-packages\web\application.py", line 246, in process
raise self.internalerror()
File "C:\Python27\lib\site-packages\web\application.py", line 515, in internalerror
parent = self.get_parent_app()
File "C:\Python27\lib\site-packages\web\application.py", line 500, in get_parent_app
if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'
Use requests library for this.
import requests
response = requests.request(method='GET', url ='http://127.0.0.1:8080/acqlock/' + resource_name)
Note: You have used port 8080 in url even though you have hosted the web.py in 8081

Django File based session - filename too long

I'm using Django==1.1 (i have to)
and file based session. I get following traceback (below)
I know that during initialization of django.contrib.sessions.backends.file.SessionStore the constructor is getting very long session_key.
But where this class instance is created - I don't know. I'm trying to find out. And why the heck so long session_key is created?
Traceback (most recent call last):
File "/Users/john/.virtualenvs/app/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 279, in run
self.result = application(self.environ, self.start_response)
File "/Users/john/.virtualenvs/app/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 651, in __call__
return self.application(environ, start_response)
File "/Users/john/.virtualenvs/app/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 245, in __call__
response = middleware_method(request, response)
File "/Users/john/.virtualenvs/app/lib/python2.7/site-packages/django/contrib/sessions/middleware.py", line 36, in process_response
request.session.save()
File "/Users/john/.virtualenvs/app/lib/python2.7/site-packages/django/contrib/sessions/backends/file.py", line 88, in save
fd = os.open(session_file_name, flags)
OSError: [Errno 63] File name too long: '/tmp/sessionid.eJydVktv4zYQ9m63Fx7aQ3-EfXJFvYWeit1FkYuLrpNuDwsQlMTQSmRJJqU4KVCgP70zQ8mPxMmhB8kWh5z55psX_33_z-7d4uaDaWv11_ez2UyW26rZvb_5wQpTVo224raUvdx9d_3Tu9lsUXacrefNsM2VEfmcdT5b2fXc9tL0AjaqvtoqWA5YMX2xw58uZIv1_Nujl3x7LAv4jeDh8HiXnjkruqf-byY61kV48PNg2k79_FUaK_dgI2ZXie957CqI4b2ef_y8voblhPVfuhRfGbMHrBIkHPb6foCrqilP4XLO9AVwHJ_kl5fINOnnPrPLmx-PTFV1r8zuw_VvbDYrirZ7EkZpBq-ibWxvhqJvDRhji6K8k41ul2W-3Lalqu3S7urlblDmif1BbyC2ECIfQGXVCMHa_E4VPRK7AsuAtQRS1nOpNZgAR4RVNWwQW2nviRsMi3ghLmSxUUQSytVjb5CYlOkTUENfASCMusM8GGXZujW9Kj9ViCE7h1bSIpC7KJEUjtiAmvX8Xj39bkplkOCALeqOh8zmYPeuhYNb2aEgou0xW6z-RJ4clbAOCFd9x1O20PAf_kE04ahRNbhTTv7cVqouLeahhwZ8zia3RC_zWpHIZ4sek5SO4AKB8UPcuxtaYGeixSc0fszI0oM4AeonJEKqEnhSQjNYgCLrSlpn6RkzVmHEABmYCzidlEBR4KP-FqkR-RNWCwEKCFBZWThMOIOIXUFm4-LQKSEf2qqUTUH1FSOaICGhupVD3QtSCOyhOIWTxIU27dCNVjJXqyNzrRFDhzUAotCbLI3SkWYU8QMIdauMAYfrVpbOTgjUaucgZmVAVvsuDE9UHUIURuhlSOQSZyOzITEbOmYXjl6M_mqFhkGbi_xWPopSdf0GjkSAN2Ku8TSlNOWp8xGfnHeJcEJ0NKbCRPIRXEQhiAj4IXMiQhzFLnL2pNxImlDZ6Ax-xuSPUipMXCF9secS3gF5VqL8cjhE0-5lhfGP_Yl6caZgytY4IA11uwedhnSGcOLIr1G3RTs0pCs6B9vFzqsraIcAPKYYxOkBeDb6nL-suaKt0f2EKi7hpyk4ifwpAlNYTookIQMJMb2RDy5mSXTWgs774n6jjGJf8b2CVdgN2F0jTAh3kkK3gR7bKOywqC6DhV9Xn7C3ER8NRQ3zOT3kc7Gp6tKoBhd9xJTSaLBD3hulRCdB1iPiNCRp5GIJeVMPJdBRNYCoQmIcVtxJZZkmhywaUzylvEgdqaAr80Z6YR95J4oaPmBnBm0ioRIhpbgCk2BfGdXLrhtZYV-mTgliAk1KcHPownxkKKOWlsE3kKKRDT1RoJ3bWfLiDMHNstfPcM97foh7rvN7_lvHAqzwtwP9kYalrBqaKuFRvxsVXszG_KaR7rqF1UCTdlxQTYtGusnuYW64-8k07D26GECu0uRHR3Alh2aJzfDSxYXjBQFf__P-wmEeXrjBcCi2i3cYDpOSZmnkfmIcgJQw2iUjxwGUy-chgCFJw9d7IwQ-dyHQp9zijMSDAZLxnFLtSDy9SeHw1I47_JuPFNt9hf0Zi59uWoAaRuoRNU7Wl6jdaOU4W19HnV1AHVAn4zRaL6N2odVnUa4xyvr1kL5-65viAQMcPIOpffQMh3d-8hmdf8aud2wqvZl6NQ_cJczdGkVlBcyk4v6JRKnzPl8Oy_8ACepvHw:1SWRxQ:eJ6zYlFnV8NFqaM2mjeYJUjvBlM'
[22/May/2012 15:45:43] "GET /login/ HTTP/1.1" 500 2736
The request.session gets constructed in SessionMiddleware.process_request, you could check in the file django/contrib/sessions/middleware.py. In early versions,(just checked in recent Django its still vulnerable), there are few checking upon the incoming session key in request.COOKIES, before touching the backend, thus you got a over-long one that fails session.save().

Python BaseHTTPServer, how do I catch/trap "broken pipe" errors?

I build a short url translator engine in Python, and I'm seeing a TON of "broken pipe" errors, and I'm curious how to trap it best when using the BaseHTTPServer classes. This isn't the entire code, but gives you an idea of what I'm doing so far:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import memcache
class clientThread(BaseHTTPRequestHandler):
def do_GET(self):
content = None
http_code,response_txt,long_url = \
self.ag_trans_url(self.path,content,'GET')
self.http_output( http_code, response_txt, long_url )
return
def http_output(self,http_code,response_txt,long_url):
self.send_response(http_code)
self.send_header('Content-type','text/plain')
if long_url:
self.send_header('Location', long_url)
self.end_headers()
if response_txt:
self.wfile.write(response_txt)
return
def ag_trans_url(self, orig_short_url, post_action, getpost):
short_url = 'http://foo.co' + orig_short_url
# fetch it from memcache
long_url = mc.get(short_url)
# other magic happens to look it up from db if there was nothing
# in memcache, etc
return (302, None, log_url)
def populate_memcache()
# connect to db, do lots of mc.set() calls
def main():
populate_memcache()
try:
port = 8001
if len(sys.argv) > 1:
port = int(sys.argv[1])
server = HTTPServer(('',port), clientThread)
#server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print '[',str(datetime.datetime.now()),'] short url processing has begun'
server.serve_forever()
except KeyboardInterrupt,SystemExit:
print '^C received, shutting down server'
server.socket.close()
The code itself works great, but started throwing errors almost immediately when in production:
Traceback (most recent call last):
File "/usr/lib/python2.5/SocketServer.py", line 222, in handle_request
self.process_request(request, client_address)
File "/usr/lib/python2.5/SocketServer.py", line 241, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.5/SocketServer.py", line 254, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.5/SocketServer.py", line 522, in __init__
self.handle()
File "/usr/lib/python2.5/BaseHTTPServer.py", line 316, in handle
self.handle_one_request()
File "/usr/lib/python2.5/BaseHTTPServer.py", line 310, in handle_one_request
method()
File "/opt/short_url_redirector/shorturl.py", line 38, in do_GET
self.http_output( http_code, response_txt, long_url )
File "/opt/short_url_redirector/shorturl.py", line 52, in http_output
self.send_response(http_code)
File "/usr/lib/python2.5/BaseHTTPServer.py", line 370, in send_response
self.send_header('Server', self.version_string())
File "/usr/lib/python2.5/BaseHTTPServer.py", line 376, in send_header
self.wfile.write("%s: %s\r\n" % (keyword, value))
File "/usr/lib/python2.5/socket.py", line 274, in write
self.flush()
File "/usr/lib/python2.5/socket.py", line 261, in flush
self._sock.sendall(buffer)
error: (32, 'Broken pipe')
The bulk of these errors seem to stem from having a problem calling the send_header() method where all I'm writing out is this:
self.send_header('Location', long_url)
So I'm curious where in my code to try to trap for this IO exception... do I write try/except calls around each of the self.send_header/self.end_headers/self.wfile.write calls? The other error I see from time to time is this one, but not sure which exception to watch to even catch this:
Traceback (most recent call last):
File "/usr/lib/python2.5/SocketServer.py", line 222, in handle_request
self.process_request(request, client_address)
File "/usr/lib/python2.5/SocketServer.py", line 241, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.5/SocketServer.py", line 254, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.5/SocketServer.py", line 522, in __init__
self.handle()
File "/usr/lib/python2.5/BaseHTTPServer.py", line 316, in handle
self.handle_one_request()
File "/usr/lib/python2.5/BaseHTTPServer.py", line 299, in handle_one_request
self.raw_requestline = self.rfile.readline()
File "/usr/lib/python2.5/socket.py", line 381, in readline
data = self._sock.recv(self._rbufsize)
error: (104, 'Connection reset by peer')
This appears to be a bug in SocketServer, see this link Python Bug: 14574
A fix (works for me in Python 2.7) is to override the SocketServer.StreamRequestHandler finish() method, something like this:
...
def finish(self,*args,**kw):
try:
if not self.wfile.closed:
self.wfile.flush()
self.wfile.close()
except socket.error:
pass
self.rfile.close()
#Don't call the base class finish() method as it does the above
#return SocketServer.StreamRequestHandler.finish(self)
The "broken pipe" exception means that your code tried to write to a socket/pipe which the other end has closed. If the other end is a web browser, the user could have stopped the request. You can ignore the traceback; it does not indicate a serious problem. If you want to suppress the message, you can put a try ... except block around all of the code in your http_output function, and log the exception if you like.
Additionally, if you want your HTTP server to process more than one request at a time, you need your server class to use one of the SocketServer.ForkingMixIn and SocketServer.ThreadingMixIn classes. Check the documentation of the SocketServer module for details.
Add: The "connection reset by peer" exception means that your code tried to read from a dead socket. If you want to suppress the traceback, you will need to extend the BaseHTTPServer class and override the handle_one_request method to add a try ... except block. You will need a new server class anyway, to implement the earlier suggestion about processing more than one request at a time.
In my application, the error didn't occur in finish(), it occurred in handle(). This fix catches the broken pipe errors:
class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
...
def handle(self):
try:
BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
except socket.error:
pass

Categories