Python SimpleHTTPServer module is a very handy tool. You can use Python SimpleHTTPServer
to turn any directory into a simple HTTP web server.
Deploy your Python applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.
Python’s http.server is a built-in module that allows you to create a simple HTTP server to serve files from a directory. It’s a convenient tool for testing, development, and sharing files over a network. It’s is not recommended for production use.
Using a simple HTTP server in Python is beneficial for several reasons:
To start a simple HTTP server in Python, use the following one-liner command in your terminal or command prompt:
python -m http.server 8000
This command starts a server on port 8000
, serving files from the current directory.
To serve files from a specific directory, navigate to that directory in your terminal or command prompt and run the server command. For example, if you want to serve files from a directory named myproject
, navigate to myproject
and run:
python -m http.server 8000
This will serve files from the myproject
directory.
By default, the server runs on port 8000
. If you want to use a different port, specify it as an argument after the server command. For example, to run the server on port 9000
:
python -m http.server 9000
Python SimpleHTTPServer
supports only two HTTP methods - GET and HEAD. So it’s a good tool to share files over network. Python SimpleHTTPServer
has been migrated to python http.server
module in python 3, we will learn about both of these modules today and see how easy it is to work with them. Suppose you and your friend are using same local network. You have some files that you want to share with your friend. But both of you have portable hard disks so that you can copy those movies to that portable hard disks and give it to your friend. Then Python SimpleHTTPServer
can help you in this case. By Using SimpleHTTPServer
, you can easily share your files to your friends who are in the same network. In this tutorial we will learn about basics of Python SimpleHTTPServer
so that you can use it your day to day life.
If you are using Windows operating system then go to your desired folder or directory that you want to share. Now, use shift+right click
. Your will find option to open command prompt in that directory. Just click on that and open command prompt there. However, if you are using Ubuntu, just right click into that directory and open terminal. After that, execute the below command.
python -m SimpleHTTPServer 9000
You can run python http server on any port, default port is 8000. Try to use port number greater than 1024 to avoid conflicts. Then open your favourite browser and type localhost:9000
. Yeah! You’re done!!! Now know your ip address and then replace localhost with your ip address and then share it with your friend.
If you are running Python 3, you will get error as No module named SimpleHTTPServer
. It’s because in python 3, SimpleHTTPServer has been merged into http.server
module. You can use below command to run python http server in Python 3.
python3 -m http.server 9000
SimpleHTTPServer
ExampleBelow images show the Python SimpleHTTPServer output in terminal and browser.
Note that if there is any
index.html
file then it will be served to the browser, otherwise directory listing will be shown as in above image.
Below image shows the terminal output for python http server module in python 3. Browser output remains same as in above images. As you can see from terminal output that the python 3 http server module is more clean, provides clear messages. Python http server module doesn’t show all the python modules details on quitting from keyboard, that is a more clean approach. That’s all about Python SimpleHTTPServer in python 2 and python http server in python 3. If you don’t have python installed in your system and want to give it a try, please go through python tutorial for beginners to get started. Reference: Official Documentation
When using Python’s http.server
, it’s essential to consider the security implications. Since the server is designed for development and testing purposes, it’s not recommended for production use. Here are some security considerations to keep in mind:
To mitigate these risks, use the server only for local development and testing, and ensure that you’re not serving sensitive data.
Here are some use cases and best practices for using Python’s http.server:
To spin up a local dev server to test front-end changes, navigate to your project directory and run:
python -m http.server 8000
This will start a server on port 8000, serving files from your project directory.
To serve static files during hackathons or project demos, navigate to the directory containing your static files and run:
python -m http.server 8000
This will allow you to quickly share your project with others over a local network.
To use Python’s http.server on a Raspberry Pi to serve files over a LAN, follow these steps:
python -m http.server 8000
http://<Raspberry Pi's IP address>:8000
.To quickly test webhooks or CORS headers, you can use Python’s http.server
to simulate a server environment. For example, you can create a simple HTML file with a webhook or CORS request and serve it using the server.
The command to start a simple HTTP server in Python 3 is:
python -m http.server 8000
This starts a server on port 8000, serving files from the current directory.
To specify a custom port, add the port number as an argument after the server command. For example, to run the server on port 9000:
python -m http.server 9000
To serve a different directory, navigate to that directory in your terminal or command prompt before running the server command. For example, if you want to serve files from a directory named myproject
, navigate to his direcory using the command cd myproject
and run:
python -m http.server 8000
This will serve files from the myproject
directory.
No, Python’s http.server
is not recommended for production use due to security concerns. It’s designed for development and testing purposes only.
To serve files over a network, not just localhost, ensure that your server is configured to listen on a network interface and that the necessary network ports are open. Additionally, you may need to configure your router to forward incoming requests to your server.
Here is a Python program that can help you serve files over a network:
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
This program will serve files from the current directory on port 8000. You can change the port number and directory as needed.
Python’s http.server
is a simple and convenient tool for serving files over a network. It’s useful for local development, file sharing, and quick prototyping. However, it’s not recommended for production use due to security concerns.
For further learning, we recommend referring to the following tutorials:
These tutorials will help you expand your knowledge of Python and its applications in networking and web development.
Continue building with DigitalOcean Gen AI Platform.
thanks pankaj i dont know why the below error is causing.while running http server command “/usr/bin/python: No module named SimpleHTTPServer” after reading your article it get clear because it was due to python3 & python3 have different command
- anonymous