Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Created September 7, 2012 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThiefMaster/3670723 to your computer and use it in GitHub Desktop.
Save ThiefMaster/3670723 to your computer and use it in GitHub Desktop.
SSL fix for flask dev server
from SocketServer import TCPServer
from OpenSSL import SSL
def patch_shutdown_request():
# Fix SocketServer's shutdown not working with pyopenssl
def my_shutdown_request(self, request):
"""Called to shutdown and close an individual request."""
try:
#explicitly shutdown. socket.close() merely releases
#the socket and waits for GC to perform the actual close.
try:
request.shutdown(socket.SHUT_WR)
except TypeError:
# ssl sockets don't support an argument
try:
request.shutdown()
except SSL.Error:
pass
except socket.error:
pass # some platforms may raise ENOTCONN here
self.close_request(request)
TCPServer.shutdown_request = my_shutdown_request
@SmittyHalibut
Copy link

This looks like it'll solve a problem I'm having, but Google hasn't helped me figure out how to APPLY this fix. Forgive me, I'm a sysadmin not a web dev so I'm mostly hacking at a Flask app written by someone else.

Where should I put a call to patch_shutdown_request()? Sometime before app.run()?

@pohlt
Copy link

pohlt commented Jun 11, 2014

Works for me (the fix proposed on http://flask.pocoo.org/snippets/111/ didn't). Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment