title: Testing HTTPS in Flask using self-signed certificates made through openssl
body: <p>I just ran the following command to generate self-signed certificates:</p>
  
  <pre><code>openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
  </code></pre>
  
  <p>After executing the commands I answered a handful of questions about my locality etc. There were no errors when generating the certificates. I then added <code>app.run(ssl_context=('cert.pem', 'key.pem'))</code> to my Flask application.</p>
  
  <pre><code>from flask import Flask
  app = Flask(__name__)
  
  @app.route('/')
  def index():
      """ Home page of the website """
      return render_template('index.html')
  
  
  ... Some other functions ...
  
  
  if __name__ == "__main__":
      app.run(ssl_context=('cert.pem', 'key.pem'))
  </code></pre>
  
  <p>The problem is that nothing is delivered over HTTPS. When I run the app I get:</p>
  
  <pre><code>$ flask run
   * Running on http://0.0.0.0:5000/
  </code></pre>
  
  <p>I can visit pages fine with HTTP but the minute I try to do HTTPS I get the following error on my browser:</p>
  
  <pre><code>An error occurred during a connection to XXXXXXX.XXX.edu:5000. 
  SSL received a record that exceeded the maximum permissible length.
  Error code: SSL_ERROR_RX_RECORD_TOO_LONG 
  </code></pre>
  
  <p>And I get the following error on my server:</p>
  
  <pre><code>code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x00\xbd\x01\x00\x00\xb9\x03\x03\xbc\x1c\xc6q#\xdcr')
  </code></pre>
  
  <p>Why can't I view content using HTTPS?</p>
  
  <p><em>Disclaimer: I know if one wants to do production level coding they should use nginx or gunicorn to serve HTTPS. I am currently trying to use Flask just for testing's sake.</em></p>