Hi! miguel
official doc recommend eventlet, but I get into trobute.
environment
python version: 3.7.3
eventlet: 0.25.0
flask-socketio: 4.1.0
exception:
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, *kwargs)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, *send_kwargs)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, *kwargs)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 603, in urlopen
chunked=chunked)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 344, in _make_request
self._validate_conn(conn)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 843, in _validate_conn
conn.connect()
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/urllib3/connection.py", line 350, in connect
ssl_context=context)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 355, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/eventlet/green/ssl.py", line 438, in wrap_socket
return GreenSSLSocket(sock, *a, _context=self, *kw)
File "/Users/zlq/work/aes/gitlab/rainbow_circle/venv/lib/python3.7/site-packages/eventlet/green/ssl.py", line 76, in __new__
args, *kw
TypeError: wrap_socket() got an unexpected keyword argument '_context'
https://github.com/eventlet/eventlet/issues/526#issue-365124783
Is there any good way? or switch to gevent
In general I've seen lots of problems when trying to work with eventlet and SSL together. If at all possible, I recommend that you terminate the SSL connection before the request reaches the server. I use nginx as reverse proxy for SSL termination.
This particular error has already been filed as an eventlet bug: https://github.com/eventlet/eventlet/issues/526.
Thank you for your response! I will be try.
Indeed the issue is with eventlet.
I decorated my Flask's password authentication with a Google reCAPTCHA decorator which basically sends a some data to Google's servers over SSL.
This is the decorator:
def verify_recaptcha(f):
@wraps(f)
def decorated_function(*args, **kwargs):
g.recaptcha_valid = None
if (
request.method == "POST"
and current_app.config["RECAPTCHA_SITE_KEY"]
and current_app.config["RECAPTCHA_SECRET_KEY"]
):
data = request.get_json() if request.get_json() else request.data
recaptcha_token = data.get("recaptcha_token")
if recaptcha_token:
r = requests.post(
"https://www.google.com/recaptcha/api/siteverify",
data={
"secret": current_app.config["RECAPTCHA_SECRET_KEY"],
"response": recaptcha_token,
"remoteip": request.access_route[0],
},
)
result = r.json()
g.recaptcha_valid = result.get("success") or False
else:
g.recaptcha_valid = False
return f(*args, **kwargs)
return decorated_function
As soon as I switched over to gevent it works as expected.
I have the same problem.
python version: 3.7.1
eventlet: 0.25.0
flask-socketio: 4.1.0
flask-mail: 0.9.1
when executing flask-mail mail.send(Message)
exception:
File "C:\GitHub*.com\back-end*_celery.py", line 16, in
mail.send(msg)
File "c:\github*.com\back-end.venv\lib\site-packages\flask_mail.py", line 491, in send
with self.connect() as connection:
File "c:\github*.com\back-end.venv\lib\site-packages\flask_mail.py", line 144, in __enter__
self.host = self.configure_host()
File "c:\github*.com\back-end.venv\lib\site-packages\flask_mail.py", line 156, in configure_host
host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
File "C:\Usersandy9\AppData\Local\Programs\Python\Python37\Lib\smtplib.py", line 1031, in __init__
source_address)
File "C:\Usersandy9\AppData\Local\Programs\Python\Python37\Lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Usersandy9\AppData\Local\Programs\Python\Python37\Lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Usersandy9\AppData\Local\Programs\Python\Python37\Lib\smtplib.py", line 1039, in _get_socket
server_hostname=self._host)
File "c:\github*.com\back-end.venv\lib\site-packageseventlet\green\ssl.py", line 440, in wrap_socket
return GreenSSLSocket(sock, a, _context=self, *kw)
File "c:\github*.com\back-end.venv\lib\site-packageseventlet\green\ssl.py", line 76, in __new__
args, *kw
TypeError: wrap_socket() got an unexpected keyword argument '_context'
*args, **kw , The problem is solvedif _is_under_py_3_7: return super(GreenSSLSocket, cls).__new__(cls) else: if not isinstance(sock, GreenSocket): sock = GreenSocket(sock) with _original_ssl_context(): ret = _original_wrap_socket( sock=sock.fd, keyfile=keyfile, certfile=certfile, server_side=server_side, cert_reqs=cert_reqs, ssl_version=ssl_version, ca_certs=ca_certs, do_handshake_on_connect=False,
args, *kw
)
ret.keyfile = keyfile
ret.certfile = certfile
ret.cert_reqs = cert_reqs
ret.ssl_version = ssl_version
ret.ca_certs = ca_certs
ret.__class__ = GreenSSLSocket
return ret
Just as an FYI to anyone else -- I actually was terminating SSL at Nginx, so for a second I was confused as to why I was getting this error. But then I realized that the managed Redis db I'm using for orchestration between flask_socketio instances required connecting via SSL/TLS (digitalocean's managed Redis), which was then causing eventlet to break down.
@miguelgrinberg Do you have a recommendation for a workaround that doesn't involve just communicating with Redis unencrypted? I'm currently running Nginx for SSL termination / load-balancing multiple gunicorn processes with one eventlet worker each on my machines. Then those machines are behind another load-balancer.
@acnebs I don't have any workarounds, sorry. The issue is currently being worked on, so hopefully a fix will be available soon.
Python 3.8. The following configuration gave an issue for me.
eventlet==0.25.2
Flask-SocketIO==4.3.0
python-socketio==4.6.0
requests==2.24.0
requests-oauthlib==1.3.0
after downgrading to requests==2.23.0 it started to work!
Posting this just if someone comes here from Google (like me was).
Python 3.8. The following configuration gave an issue for me.
eventlet==0.25.2 Flask-SocketIO==4.3.0 python-socketio==4.6.0 requests==2.24.0 requests-oauthlib==1.3.0after downgrading to
requests==2.23.0it started to work!Posting this just if someone comes here from Google (like me was).
Hm I have same dependencys except the requests I do not have at all.... Nevertheless the same error occurs for me. I also removed ssl.py and now everything works....
Most helpful comment
In general I've seen lots of problems when trying to work with eventlet and SSL together. If at all possible, I recommend that you terminate the SSL connection before the request reaches the server. I use nginx as reverse proxy for SSL termination.
This particular error has already been filed as an eventlet bug: https://github.com/eventlet/eventlet/issues/526.