Skip to content

Instantly share code, notes, and snippets.

@manuthu
Last active January 26, 2018 09:43
Show Gist options
  • Save manuthu/b6e3306058386bccc66aff40646568f0 to your computer and use it in GitHub Desktop.
Save manuthu/b6e3306058386bccc66aff40646568f0 to your computer and use it in GitHub Desktop.
Simple Python SSL Server
"""
Generate a server.key and server.cert.
>> mike:tmp$ openssl req -nodes -new -x509 -keyout server.key -out server.cert
Combine the generated keys.
>> mike:tmp$ cat server.key server.cert > server.pem
Now run the script
"""
import BaseHTTPServer, SimpleHTTPServer
import ssl
certfile = './server.pem'
host = '0.0.0.0'
port = 9000
httpd = BaseHTTPServer.HTTPServer((host, port), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile=certfile, server_side=True)
print 'Accepting ssl connection Host:%s Port:%s ' % (host, port)
httpd.serve_forever()
@manuthu
Copy link
Author

manuthu commented Jan 26, 2018

To create an ssl server:

import logging
from flask import Flask, request, current_app


logging.basicConfig(level=logging.DEBUG)

app = Flask(__name__)


@app.route('/', methods=['POST', 'GET'])
def index():
    current_app.logger.info('DATA %s', request.data)
    current_app.logger.info('Form %s', request.form)
    return 'Received'


if __name__ == "__main__":
    context = ('./server.cert', './server.key')
    app.run(
        host='0.0.0.0',
        port=9999,
        ssl_context=context,
        debug=True)

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