IO Error - Flask-mail and running server with proxy - python

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.

Related

openbsd httpd with gunicorn+uvicorn -- Remote protocol error : illegal request line

My web deployment setup is on openBSD and consists of httpd on front with guicorn + uvicorn as the back engine, connected via unix socket.
The setup works, in the sense that requests from httpd are being forwarded to gunicorn over the unix sockets. However, the gunicorn/uvicorn is not able to understand the incoming http request.
The error stack
[2021-11-22 22:52:17 +0530] [1631] [WARNING] Invalid HTTP request received.
Traceback (most recent call last):
File "/home/shared/Builds/Python-3.10.0/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 136, in handle_events
event = self.conn.next_event()
File "/home/shared/Builds/Python-3.10.0/lib/python3.10/site-packages/h11/_connection.py", line 443, in next_event
exc._reraise_as_remote_protocol_error()
File "/home/shared/Builds/Python-3.10.0/lib/python3.10/site-packages/h11/_util.py", line 76, in _reraise_as_remote_protocol_error
raise self
File "/home/shared/Builds/Python-3.10.0/lib/python3.10/site-packages/h11/_connection.py", line 425, in next_event
event = self._extract_next_receive_event()
File "/home/shared/Builds/Python-3.10.0/lib/python3.10/site-packages/h11/_connection.py", line 367, in _extract_next_receive_event
event = self._reader(self._receive_buffer)
File "/home/shared/Builds/Python-3.10.0/lib/python3.10/site-packages/h11/_readers.py", line 68, in maybe_read_from_IDLE_client
raise LocalProtocolError("illegal request line")
h11._util.RemoteProtocolError: illegal request line
I am not sure what are potential causes for illegal request line.
httpd is not support http proxying.
It is support serving static files as well as FastCGI. And error message, indicate that your httpd try to communicate with gunicorn using FastCGI.
So, if you stick to httpd, find a way to run your app using FastCGI server instead of WSGI (gunicorn). Many years ago flup was a popular choice.
Or, just use Nginx instead of httpd.

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).

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

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

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