Python Bottle framework becoming non-responsive - python

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"

Related

Constant python/django errors

Does anyone know what I should do? I receive this error everytime I interact with my localhost.
It does not crash, my terminal just blows up with these exceptions. Does anyone know how I should proceed with this issue?
I know this is alot of errors but I am starting to learn more about Django so your help would be greatly appreciated. Thank you!
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50979)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50974)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50978)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50980)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50981)
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 174, in handle
self.handle_one_request()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer
----------------------------------------
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 174, in handle
self.handle_one_request()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 174, in handle
self.handle_one_request()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer
----------------------------------------
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 174, in handle
self.handle_one_request()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer
----------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 174, in handle
self.handle_one_request()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer
----------------------------------------
----------------------------------------
Not Found: /favicon.ico
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50982)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 174, in handle
self.handle_one_request()
File "/Users/Emmanuel/.local/share/virtualenvs/thepillow-kX0pGe6_/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer
'''
The issue is raised by the fav.ico because django server is always resetting after not finding a favicon so If you add one. Even though If you never explicitly load one, django try and load a default one from the root of the project. you can use favicon io to make a simple fav icon. you put it inside your static folder and load it like that :
{% load static %}
<head>
<link rel="shortcut icon" href="{% static 'images/favicon.ico' %}" />

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

Django "Yellow Page" errors not showing up in Chrome

I have Django v 1.6 stable and Python 2.7.5 64-bit. When I have an error occur in one of my views, a blank page is displayed in Chrome and the following traceback printed in my console:
Traceback (most recent call last):
File "C:\WinPython\python-2.7.5.amd64\lib\SocketServer.py", line 593, in proce
ss_request_thread
self.finish_request(request, client_address)
File "C:\WinPython\python-2.7.5.amd64\lib\SocketServer.py", line 334, in finis
h_request
self.RequestHandlerClass(request, client_address, self)
File "C:\WinPython\python-2.7.5.amd64\lib\site-packages\django-1.6-py2.7.egg\django\core\servers\basehttp.py", line 126, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\WinPython\python-2.7.5.amd64\lib\SocketServer.py", line 651, in __ini
t__
self.finish()
File "C:\WinPython\python-2.7.5.amd64\lib\SocketServer.py", line 710, in finis
h
self.wfile.close()
File "C:\WinPython\python-2.7.5.amd64\lib\socket.py", line 279, in close
self.flush()
File "C:\WinPython\python-2.7.5.amd64\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in yo
ur host machine
However, when I use IE, the yellow error page displays as intended.

Handle one HTTP request from python with timeout

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

Categories