Python SimpleHTTPServer on Windows (python -m http.server)
Codemia Knowledge Hub ·
Python's SimpleHTTPServer module serves the files in a directory over HTTP with a single command. It is part of the standard library, so there is nothing to install. In Python 3 it was merged into the http.server module, so on a current Python install that is the module you run.
It is handy for testing, sharing files on a local network, or standing up a quick web service.
Prerequisites
- Python installed: download it from python.org and confirm the install with
python --versionin the command prompt. - Command prompt access: basic familiarity with running commands.
Starting the server
Step 1: Open the command prompt
Press Win + R, type cmd, and press Enter.
Step 2: Go to the directory to serve
Use cd to move into the directory you want to serve, then start the server:
On Python 2 the command is python -m SimpleHTTPServer 8000.
- Replace
path\to\your\directorywith the path of the directory you want to serve. You can copy the path from Windows Explorer. 8000is the port number. Replace it with any valid port if needed.
Things to know
- Module name:
SimpleHTTPServerwas the Python 2 name. In Python 3 it is part ofhttp.server. - Which directory is served: the server serves the current directory by default. To serve a different one,
cdinto it first. - Port number: pick a free port (above 1024 if you don't have administrative privileges).
- Security: the server is meant for quick, temporary use. It lacks robust security features and should not be used in production.
- Stopping the server: press
Ctrl + Cin the command prompt where it is running. - File access: make sure the files you want to serve are in the served directory, or use relative paths to reach them.
- Running scripts: you can run a Python script directly by going to its directory and calling
python script_name.py.
Related reading
- Set value for particular cell in pandas DataFrame using index
- Set variable from another variable in Jinja
- set 'x-message-ttl' in pika python
- setting an environment variable in virtualenv
- Setting Django up to use MySQL
- Setting the correct encoding when piping stdout in Python
- Setting up a multi-threaded Pyro project
- setup script exited with error command 'x86_64-linux-gnu-gcc' failed with exit status 1
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.