AttributeError: 'Context' object has no attribute 'wrap_socket'

Question:

I am trying to set up a Flask server that uses an OpenSSL context.
However, since I moved the script on a different server, it keeps throwing the following error, no matter if I am using Python 2.7 or 3.4 and no matter which SSL method I chose (SSLv23 / TLSv1/…):

  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.4/threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 602, in inner
    passthrough_errors, ssl_context).serve_forever()
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 506, in make_server
    passthrough_errors, ssl_context)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 450, in __init__
    self.socket = ssl_context.wrap_socket(self.socket,
AttributeError: 'Context' object has no attribute 'wrap_socket'

The according code below:

if __name__ == "__main__":
        context = SSL.Context(SSL.SSLv23_METHOD)
        context.use_privatekey_file('key.key')
        context.use_certificate_file('cert.crt')
        app.run(host='0.0.0.0', port=80, ssl_context=context, threaded=True, debug=True)

Thank you very much in advance! I am happy for any help

Asked By: Nils Milchert

||

Answers:

As of 0.10, Werkzeug doesn’t support OpenSSL contexts anymore. This decision was made because it is easier to support ssl.SSLContext across Python versions. Your option to re-write this code is this one:

if __name__ == "__main__":
    context = ('cert.crt', 'key.key')
    app.run(host='0.0.0.0', port=80, ssl_context=context, threaded=True, debug=True)

See http://werkzeug.pocoo.org/docs/latest/serving/ for all possibilities.