So I am creating a server, and making a request to it, all in the same file.
This works properly:
import gevent
import gevent.monkey
gevent.monkey.patch_all()
import requests
from gevent.pywsgi import WSGIServer
from flask import Flask
app = Flask(__name__)
app.debug = True
# Simple catch-all server
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST'])
@app.route('/<path:path>', methods=['GET', 'POST'])
def catch_all(path):
return 'It is Working!'
if __name__ == '__main__':
http_server = WSGIServer(('', 8080), app)
srv_greenlet = gevent.spawn(http_server.start)
gevent.sleep(0.5)
resp = requests.get('http://127.0.0.1:8080/')
print resp.text
http_server.stop()
gevent.joinall([srv_greenlet])
Output:
(py2sni)user@host$ python question.py
127.0.0.1 - - [2013-10-01 11:48:46] "GET / HTTP/1.1" 200 130 0.000614
It is Working!
But this blocks, and I have to kill the process externally:
import gevent
import gevent.monkey
gevent.monkey.patch_all()
import requests
from gevent.pywsgi import WSGIServer
from flask import Flask
app = Flask(__name__)
app.debug = True
# Simple catch-all server
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST'])
@app.route('/<path:path>', methods=['GET', 'POST'])
def catch_all(path):
return 'It is Working!'
if __name__ == '__main__':
http_server = WSGIServer(('', 4430), app, keyfile='server.key', certfile='server.crt')
srv_greenlet = gevent.spawn(http_server.start)
gevent.sleep(0.5)
resp = requests.get('https://127.0.0.1:4430/')
print resp.text
http_server.stop()
gevent.joinall([srv_greenlet])
The only difference in these two scripts is that one uses SSL and the other does not. Where am I making a mistake here? If it helps, I am using gevent 0.13.8 and requests 2.0
The problem lies here.
It only occurs when using pyopenssl and gevent/greenlet. Because the retry of the handshake has no IO the control is never given to the greenlet running flask, so the handshake can never succeed.
I have opened a bugreport/pullrequest for this over at shazow/urllib3#250. It should be straightforward to backport yourself or wait for the next minor release of requests.