Flask app get "IOError: [Errno 32] Broken pipe" - python

Now I use flask to develop web app.
But at first it works well,after operating web page for a while,the flask back-end shows error like these:
File "/usr/lib64/python2.6/BaseHTTPServer.py", line 329, in handle
self.handle_one_request()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 251, in handle_one_request
return self.run_wsgi()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 193, in run_wsgi
execute(self.server.app)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 184, in execute
write(data)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 152, in write
self.send_header(key, value)
File "/usr/lib64/python2.6/BaseHTTPServer.py", line 390, in send_header
self.wfile.write("%s: %s\r\n" % (keyword, value))
IOError: [Errno 32] Broken pipe
My app run on port 5000 app.run(debug=True,port=5000),
I use nginx as web server,and set proxy_pass http://127.0.0.1:5000 in nginx config file.
Now I really don't know where is the wrong,I use session['email'] = request.form['email'] and in other file I use email = session.get('email').
Is this usage right? How to set session active period?
or any other reason cause this error ?
then I set app.run(debug=False,port=5000),it shows new error
File "/usr/lib64/python2.6/SocketServer.py", line 671, in finish
self.wfile.flush()
File "/usr/lib64/python2.6/socket.py", line 303, in flush
self._sock.sendall(buffer(data, write_offset, buffer_size))
socket.error: [Errno 32] Broken pipe
why ?
Please help me,thks.

The built-in werkzeug server is not capable of handling the remote end closing the connection while the server is still churing its content out.
instead of app.run(debug=True,port=5000)
try
from gevent.wsgi import WSGIServer
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
or if you are using nginx, use it with uwsgi as described here
It is rather a werkzeug issue I would argue

Related

How to avoid port error already used in web framework bottle?

In Python3 I have a program to test the web framework bottle:
from bottle import route, run
#route('/')
def index():
return "olá pessoas"
if __name__ == '__main__':
run()
I am running the programs inside a virtualenv, in the command line. But there is this error:
(live-de-python) reinaldo#reinaldo-Inspiron-5567:~/Documentos/Code/live-de-python/repo$ python3 basic_bottle.py
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://127.0.0.1:8080/
Hit Ctrl-C to quit.
Traceback (most recent call last):
File "basic_bottle.py", line 11, in <module>
run()
File "/home/reinaldo/Documentos/Code/live-de-python/lib/python3.6/site-packages/bottle.py", line 3127, in run
server.run(app)
File "/home/reinaldo/Documentos/Code/live-de-python/lib/python3.6/site-packages/bottle.py", line 2781, in run
srv = make_server(self.host, self.port, app, server_cls, handler_cls)
File "/usr/lib/python3.6/wsgiref/simple_server.py", line 153, in make_server
server = server_class((host, port), handler_class)
File "/usr/lib/python3.6/socketserver.py", line 453, in __init__
self.server_bind()
File "/usr/lib/python3.6/wsgiref/simple_server.py", line 50, in server_bind
HTTPServer.server_bind(self)
File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.6/socketserver.py", line 467, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
I'm using Ubuntu. Does the error say that port 8080 is already in use? But I have no other program open
Please, does anyone know what it can be?
The port 8080 is already used by an other process. You probably launched the script and forgot to end it. Check with
$ ps -aux | grep basic_bottle
Or simply change the port with run(port=8090).

IO Error - Flask-mail and running server with proxy

I'm with a very very wierd bug...
I have a flask app using flask-mail to send email messages.
In a RedHat Server, I tryied using runserver (flask-manager) and gunicorn. So I have a apache server connecting to this app using Proxy.
When I run the app, using any user (root or other), the app runs and it sends emails normally.
But when i close the session with the server (exit in terminal) it stops to send mail and gives me this stack trace:
in send_mail
return mail.send(msg)
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 415, in send
with self.connect() as connection:
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 123, in __enter__
self.host = self.configure_host()
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 144, in configure_host
host.login(self.mail.username, self.mail.password)
File "/usr/local/lib/python2.7/smtplib.py", line 575, in login
self.ehlo_or_helo_if_needed()
File "/usr/local/lib/python2.7/smtplib.py", line 535, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
File "/usr/local/lib/python2.7/smtplib.py", line 406, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "/usr/local/lib/python2.7/smtplib.py", line 336, in putcmd
self.send(str)
File "/usr/local/lib/python2.7/smtplib.py", line 320, in send
print>>stderr, 'send:', repr(str)
IOError: [Errno 5] Input/output error
Running with manager:
python myapp.py
Running with gunicorn I use:
gunicorn -w 2 -b 0.0.0.0:8388 myapp:app
I'm really stuck here.. as i tested using 2 different containers... i do not have any other ideias to solve it... using wsgi i could not make it work on this server cause the lib do not install at all =(
any other ideas?
thanks!
Looking at smtplib source (https://hg.python.org/cpython/file/2.7/Lib/smtplib.py#l324), it looks like what's happening is you're trying to write to stderr, which may be the source of the I/O error when running under a server.
If you're setting SMTP(...).debuglevel anywhere, try removing that line.

error 10054 while trying to download binary file through ftp in python 2.7

I'm pretty new to python, so this very well be user error here, but I'm not quite sure what to do from here.
I'm trying to login to a zyxel gs2200 switch via ftp and download it's config file.
The command through cmd ftp is
get config X.log where X.log is whatever you decide to name it.
In python I can log in just fine, but I can not download a file without throwing an exception.
import ftplib
ftp = ftplib.FTP("my.ip.here")
ftp.login('user','Pass')
'230 Logged in'
ftp.retrbinary('RETR config', open('config', 'wb').write)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
File "C:\Python27\lib\ftplib.py", line 398, in retrbinary
self.voidcmd('TYPE I')
File "C:\Python27\lib\ftplib.py", line 248, in voidcmd
self.putcmd(cmd)
File "C:\Python27\lib\ftplib.py", line 178, in putcmd
self.putline(line)
File "C:\Python27\lib\ftplib.py", line 173, in putline
self.sock.sendall(line)
File "C:\Python27\lib\socket.py", line 222, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
I've also tried opening a file before and then just calling it in the method, but it still gives the same error.
does anyone have any idea on how I can get these config files in python?
I Can tell you easy method to do this.
Ftp Login Url: ftp://username:password#hostname/
Username: your Username
Password: Your Password
To download a file from FTP server you could:
import urllib
urllib.urlretrieve('ftp://username:password#hostname/yourconfigname', 'yourconfigname')
In your case you can do something like this.
===============================================================
As for your problem the python error clearly indicating that you did not establish connection correctly(Always Close Connection) or May be your destination had already connected to maximum destinations thats why your target destination closing your connection.

how can fix this error googleApp engine error 10054

i am using google app engine on my server (listen --address=0.0.0.0 all network) my application a short time correctyl(i get some users input, some process,show the data and insert mysql db) after i take this error message and web browser didn't show anything, my system: google app engine, python 2.7, mysql(via rdbms) i change the port(8080,8091,8090vs...) or restart app engine, my applicayion again work but after then again same error message
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:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2734, in __init__
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
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 10054]
Try switching to a more serious networking framework, not based on a toy HTTP server (BaseHTTPServer is a toy HTTP server).
10054 is the Windows error ECONNRESET. This indicates that the connection your HTTP server is trying to read from has been closed. It's not exactly an error condition - connections get closed, it's a normal part of their lifecycle - but the Google AppEngine (development!) server you're using seems to be treating it as an error. Perhaps in doing so it ends up responding incorrectly to other requests as well.
A correct HTTP server will not have a problem dealing with this situation.
The AppEngine development server is indeed pretty limited (single-threaded, HTTP 1.0 only, not robust to connection resets), but it is the only option for emulating in a local dev. environment the behavior of the production server, including stubs to AppEngine services and sandboxing of some modules (including os and socket).
Until Google provide us with a more robust alternative, I added exception handlers for the expected socket errors in the handle_one_request method of BaseHTTPServer.py (located in the Lib subdir of the Python installation directory)
[end of BaseHTTPServer.py::handle_one_request() - Python 2.7]
except socket.error as socket_error:
import errno
if socket_error.errno in (errno.ECONNABORTED, errno.WSAECONNABORTED, errno.ECONNRESET, errno.WSAECONNRESET):
pass
else:
raise
ECONNABORTED/WSAECONNABORTED is happening on remote disconnects - see Python issue 14574 for details.

how to send email in django? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Django - [Errno 111] Connection refused
I have found a a link on how to send email with django : https://docs.djangoproject.com/en/dev/topics/email/
i followed the example and i got this:
>>> from django.core.mail import send_mail
>>> send_mail('dfdf', 'dfdfdf', 'logadrinab#agile.com.ph', ['ddcahanap#agile.com.ph'])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 251, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 79, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib/python2.7/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 111] Connection refused
>>>
i think it's because of EMAIL_HOST and EMAIL_PORT, if it is so
where can I find it and how to use it? If it is not can anyone give me an idea on how to send email in django?
thanks in advance...
In your settings.py for your project, make sure you set your EMAIL_HOST, EMAIL_PORT and other settings (like a username or password if your host requires authentication).
Also, make sure you are running the console using the manage.py shell command, and not your normal python shell, so django loads the settings correctly.
You are getting an error because you have no mail transfer agent installed on you system.
You just need to install postfix package in default configuration (it will ask you some questions during install, just choose defaults).
And there is no need to altering your configuration or settings.py or whatever.
You'll need to correctly configure the SMTP email backend for your current host and test it interactively like you just did.
It's possible that they're using TLS or an alternate port--you should look it up in your account settings. Alternatively you can configure Django to use a remote service host, like Gmail, to get yourself started if you have no other options.
You are right, you need to specify correct host and port in settings.py.
What server you are going to use? If it is your hosting provider's server, for example, it is better to contact hosting support to get this info.

Categories