Instantly share code, notes, and snippets.

Embed
What would you like to do?
# taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
# generate server.xml with the following command:
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
# run as follows:
# python simple-https-server.py
# then in your browser, visit:
# https://localhost:4443
import BaseHTTPServer, SimpleHTTPServer
import ssl
httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
httpd.serve_forever()
@JerryFox1

This comment has been minimized.

JerryFox1 commented Jul 16, 2015

I love you. This is so simple to use ! lol

@stephenlb

This comment has been minimized.

stephenlb commented Dec 9, 2015

Thank you!

@FrancescoSaverioZuppichini

This comment has been minimized.

FrancescoSaverioZuppichini commented Mar 3, 2016

Thank you!

@jcfigueiredo

This comment has been minimized.

jcfigueiredo commented Apr 11, 2016

Thank you!

@genbtc

This comment has been minimized.

genbtc commented Apr 16, 2016

Thank you. even worked on windows. just had to locate an openssl.exe binary (was stored in a github folder)

@c3ry5

This comment has been minimized.

c3ry5 commented Apr 18, 2016

This is great thanks

@ulkoart

This comment has been minimized.

ulkoart commented May 17, 2016

Thank you!

@socketpair

This comment has been minimized.

socketpair commented Jun 8, 2016

Thank you!

@liuyang1

This comment has been minimized.

liuyang1 commented Jun 27, 2016

It works!, Thanks!

@jakejone

This comment has been minimized.

jakejone commented Jul 11, 2016

666666666666666666

@Sujay18

This comment has been minimized.

Sujay18 commented Jul 15, 2016

I want https code using python 2.7 on windows can anybody help me with that????

@user454322

This comment has been minimized.

user454322 commented Sep 27, 2016

Thanks!

@gurusrikar

This comment has been minimized.

gurusrikar commented Sep 28, 2016

This is amazing. Thank you!

@orblivion

This comment has been minimized.

orblivion commented Nov 24, 2016

Maybe want to put the private key in the parent directory so it doesn't get served :-P Awesome, though. Thanks!

@lymons

This comment has been minimized.

lymons commented Dec 21, 2016

Thank you!

@pageauc

This comment has been minimized.

pageauc commented Jan 5, 2017

Here is a more advanced version FYI ... Note I wrote this as part of my pi-timolo project but it can run as a stand alone web server. See wiki and repo code for more details

https://github.com/pageauc/pi-timolo/wiki/Access-images-via-webserver

@syscools

This comment has been minimized.

syscools commented Feb 2, 2017

Thank you so much.

@einsteinarbert

This comment has been minimized.

einsteinarbert commented Feb 6, 2017

here is fix code for test https on python 35-32 on windows 10

from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler
import ssl

httpd = HTTPServer(('localhost', 4443), SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket,
certfile='server.pem', server_side=True)

httpd.serve_forever()

@johnyradio

This comment has been minimized.

johnyradio commented Mar 10, 2017

Works great on Mac, except the cert created is not getting correctly trusted. Dragging the cert into Keychain Access and setting 'Trust Always', allows the page to load, but getting "Not secure". How to make secure?

@mihirsam

This comment has been minimized.

mihirsam commented Apr 16, 2017

Not working on ubuntu. Please help

@tomasdev

This comment has been minimized.

tomasdev commented Apr 21, 2017

THANK YOU

@venkatesh828

This comment has been minimized.

venkatesh828 commented May 3, 2017

thank you... it's working

@mockingtao

This comment has been minimized.

mockingtao commented May 4, 2017

Hi, I have excute the follow steps on my macbook:
1、openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
2、python simple-https-server.py

but, there's something wrong,here is the log:
File "simple-https-server.py", line 13, in
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 911, in wrap_socket
ciphers=ciphers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 522, in init
self._context.load_cert_chain(certfile, keyfile)
IOError: [Errno 2] No such file or directory

someone helped me?Thank you...

@TonyDiana

This comment has been minimized.

TonyDiana commented May 11, 2017

Thank you!
Muchas gracias

@zhangsu

This comment has been minimized.

zhangsu commented May 16, 2017

Thanks!

Minor type on line 2:

# generate server.xml with the following command:

Should be

# generate server.pem with the following command:
@lizhaojie001

This comment has been minimized.

lizhaojie001 commented Jun 14, 2017

Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 151, in _run_module_as_main
mod_name, loader, code, fname = _get_module_details(mod_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 101, in _get_module_details
loader = get_loader(mod_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 464, in get_loader
return find_loader(fullname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 474, in find_loader
for importer in iter_importers(fullname):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 430, in iter_importers
import(pkg)
File "simple-https-server.py", line 12
SyntaxError: Non-ASCII character '\xe2' in file simple-https-server.py on line 12, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

@talhasch

This comment has been minimized.

talhasch commented Jul 8, 2017

By inspired from this gist, i have developed a python command line application for creating instant simple https server. Check this out https://github.com/talhasch/pyhttps

@jespertheend

This comment has been minimized.

jespertheend commented Jul 16, 2017

Thanks! This is very good, the only thing that I need that was missing was support for POST requests in order to support the facebook canvas.
I wrote a fix here, it basically handles POST requests as if they were GET requests. The only difference is it doesn't give an error anymore.

@fadyosman

This comment has been minimized.

fadyosman commented Jul 17, 2017

Love it man, I was afraid I will have to rewrite my script to include SSL but this awesome one line modification :D

@RichardBronosky

This comment has been minimized.

RichardBronosky commented Sep 7, 2017

To use this with a Let's Encrypt (aka certbot) certificate instead of a self-signed, you will need to pass both certfile= and keyfile= to ssl.wrap_socket(). I have a complete demo of this in my fork: https://gist.github.com/RichardBronosky/644cdfea681518403f5409fa16823c1f

@philipsahli

This comment has been minimized.

philipsahli commented Dec 6, 2017

Great, thanks! You saved my day for an ugly workaround.

@elgs

This comment has been minimized.

elgs commented Dec 23, 2017

I got the following error:

Traceback (most recent call last):
File "http-server.py", line 1, in
import BaseHTTPServer, SimpleHTTPServer
ModuleNotFoundError: No module named 'BaseHTTPServer'

@moppymopperson

This comment has been minimized.

moppymopperson commented Jan 2, 2018

@elgs You're probably using Python 3.X. Dropping back to 2.X will solve your issue.

@Cediddi

This comment has been minimized.

Cediddi commented Feb 15, 2018

Please don't drop to python2, instead change to this;

import http.server
import ssl

httpd = http.server.HTTPServer(('localhost', 4443), http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
httpd.serve_forever()

Just two lines of change. Don't be afraid of Python 3.

@mitchellorsucci

This comment has been minimized.

mitchellorsucci commented Feb 28, 2018

Thank you so much.
I know nothing about server side in Python and this example got me up and running immediately

@tooraj

This comment has been minimized.

tooraj commented Mar 9, 2018

I get "Invalid certificate" message on Chrome and other browsers. Do I have to buy a certificate to get rid of this message?

@mpetronic

This comment has been minimized.

mpetronic commented Mar 12, 2018

Thank you. Exactly what I needed to mock an SSL server for some F5 configuration testing. :)

@Wolf-at-SO

This comment has been minimized.

Wolf-at-SO commented Mar 20, 2018

@tooraj You don't have to by a certificate. Use Let's Encrypt, see comment by RichardBronosky

@elfgoh

This comment has been minimized.

elfgoh commented May 10, 2018

With reference to this I created a python3 version. Thanks!

@okaapi

This comment has been minimized.

okaapi commented Jul 11, 2018

What am I doing wrong - I get "192.168.86.xxx refused to connect" in Chrome (from another machine), and

ERROR: The certificate of ‘127.0.0.1’ is not trusted.
ERROR: The certificate of ‘127.0.0.1’ hasn't got a known issuer.
The certificate's owner does not match hostname ‘127.0.0.1’

I created the server.pem using the openssl command in the header...

@elihe999

This comment has been minimized.

elihe999 commented Jul 12, 2018

Thanks, Is that possible to add authentication function like the DummyAuthorizer() on DummyAuthorier from python package ‘pyftpdlib’. I thinking about add a user name and password then make a simple https file system.

@telmotrooper

This comment has been minimized.

telmotrooper commented Jul 26, 2018

I ported it to Python 3, if anyone wants it: https://gist.github.com/telmotrooper/84d8d4afbb294b599c6f443bbb36a456

@pdscopes

This comment has been minimized.

pdscopes commented Jul 30, 2018

@okaapi

What am I doing wrong - I get "192.168.86.xxx refused to connect" in Chrome (from another machine)

The certificate you have created is self-signed. By default web browsers will not trust self-signed certificates. You should be able to select an option ignore the warning and to connect to the site anyway. If you want the certificate to be trusted you will need to do one of a few things:

  1. Expose the site externally and use letsecrypt to get a free trusted certificate.
  2. Follow a tutorial to create your own CA (certificate authority) and use that to sign your certificates, then install you CA on every device you connect to your site with
  3. Except the fact that you will get that warning every now and then (or permanently accept the certificate)

The certificate's owner does not match hostname ‘127.0.0.1’

This is to do with the certificate's CN (common name) which openssl sets as localhost/127.0.0.1 if you don't explicitly set it when creating a certificate (https://support.dnsimple.com/articles/what-is-common-name/).

@ds-hwang

This comment has been minimized.

ds-hwang commented Sep 28, 2018

❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️

I'd like to access it by another machine. here's solution based on @telmotrooper 's code

#!/usr/bin/env python3

# Ported to Python 3 by Telmo "Trooper" (telmo.trooper@gmail.com)
# 
# Original code from:
# http://www.piware.de/2011/01/creating-an-https-server-in-python/
# https://gist.github.com/dergachev/7028596
# 
# To generate a certificate use:
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes

from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl

separator = "-" * 80
port = 4443
httpd = HTTPServer(("", port), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile="/home/dshwang/bin/https/server.pem", server_side=True)

print(separator)
print("Server running on https://localhost:" + str(port))
print(separator)

httpd.serve_forever()
@bars0um

This comment has been minimized.

bars0um commented Nov 2, 2018

Nice!!

@Remydeme

This comment has been minimized.

Remydeme commented Nov 5, 2018

Awesome thanks.

@guyo13

This comment has been minimized.

guyo13 commented Nov 11, 2018

Hello
I made a modification of this so that the private key is not saved in the same file as the certificate.
You can view it here: https://gist.github.com/guyo13/9f3955de7fb20927d8e9218c1dcd2691

The modified openssl command:
openssl req -new -x509 -keyout key.pem -out server.pem -days 365 -nodes

And in the python code you have to specify the key using the keyword argument 'keyfile':

httpd.socket = ssl.wrap_socket (httpd.socket, keyfile='./key.pem', certfile='./server.pem', server_side=True)

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