Sometimes it is handy to just have simple local web server that serves a web directory in a non complex way. To be used in a web framework development, in a web site development, or just for validating the HTML part of the project documentation.
As with python3
the module has changed from python2
: http.server
.
python3 -m http.server 8000
Point your web browser to http://127.0.0.1:8000
or http://localhost:8000
.
If you want to be more explicit: py-server-http
import os
from http.server import HTTPServer, CGIHTTPRequestHandler
os.chdir('.') # Current directory, change it if needed
server_object = HTTPServer(server_address=('', 8000),
RequestHandlerClass=CGIHTTPRequestHandler)
server_object.serve_forever()
In case SSL support is important:
openssl req -x509 -newkey rsa:2048 -keyout /path/to/key.pem \
-out /path/to/cert.pem -days 365
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl
os.chdir('.') # Current directory, change it if needed
httpd = HTTPServer(('localhost', 4443), BaseHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket,
keyfile="/path/to/key.pem",
certfile='/path/to/cert.pem', server_side=True)
httpd.serve_forever()
Point your web browser to https://127.0.0.1:4443
or https://localhost:4443
.
A simple web server can be used with Docker to expose html
pages to the outside world for testing purposes.
FROM python:3.9.2
VOLUME ["/code"]
ADD . /code
WORKDIR /code
EXPOSE 5123
CMD ["python", "-m", "http.server", "5123"]
Point your web browser to http://127.0.0.1:5123
or http://localhost:5123
.
Go into the web directory you want to serve and execute python2
with SimpleHTTPServer
.
python -m SimpleHTTPServer 8000
Point your web browser to http://127.0.0.1:8000
or http://localhost:8000
.
Version | Date | Notes |
---|---|---|
0.1.2 | 2023-01-18 | Improve writing, add SSL and Docker examples |
0.1.1 | 2022-07-13 | Format for quick guide, update for python3 |
0.1.0 | 2016-07-19 | Initial release (as 0.1) |