Created
November 5, 2020 01:26
-
-
Save stephenbradshaw/a2b72b5b58c93ca74b54f7747f18a481 to your computer and use it in GitHub Desktop.
Python 3 Simple HTTPS server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# python3 update of https://gist.github.com/dergachev/7028596 | |
# Create a basic certificate using openssl: | |
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes | |
# Or to set CN, SAN and/or create a cert signed by your own root CA: https://thegreycorner.com/pentesting_stuff/writeups/selfsignedcert.html | |
import http.server | |
import ssl | |
httpd = http.server.HTTPServer(('127.0.0.1', 443), http.server.SimpleHTTPRequestHandler) | |
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True) | |
httpd.serve_forever() |
Thanks!
I am on Python 3.11.2 and I see a warning when running this script:
python3_https_server.py:11: DeprecationWarning: ssl.wrap_socket() is deprecated, use SSLContext.wrap_socket()
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
I fix this warning with the following script:
#!/usr/bin/env python3
# python3 update of https://gist.github.com/dergachev/7028596
# Create a basic certificate using openssl:
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
# Or to set CN, SAN and/or create a cert signed by your own root CA: https://thegreycorner.com/pentesting_stuff/writeups/selfsignedcert.html
import http.server
import ssl
httpd = http.server.HTTPServer(('127.0.0.1', 443), http.server.SimpleHTTPRequestHandler)
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ctx.load_cert_chain(certfile='./server.pem')
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True)
httpd.serve_forever()
Thanks @lxylxy123456
If server.crt
server.key
are separate, use this to load
ctx.load_cert_chain(certfile='server.crt', keyfile='server.key')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome!
If you want to expose it to the world, just change
'127.0.0.1'
to'0.0.0.0'