Tutorial

How to Create a Simple HTTP Server in Python

Updated on April 19, 2025

Sr Technical Writer

How to Create a Simple HTTP Server in Python

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.

What is Python’s http.server?

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.

Why Use a Simple HTTP Server in Python?

Using a simple HTTP server in Python is beneficial for several reasons:

  • Easy file sharing: Share files with others on the same network without setting up a full-fledged web server.
  • Quick testing: Test web applications or static websites without the need for a complex server setup.
  • Development convenience: Serve files from a directory for local development or testing purposes.

How to Start a Server (One-liner)

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.

Serving a Specific 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.

Choosing a Port

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

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.

Python Simple HTTP Server

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.

Python SimpleHTTPServer Error - No module named SimpleHTTPServer

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

Python SimpleHTTPServer Example

Below images show the Python SimpleHTTPServer output in terminal and browser. Python SimpleHTTPServer, Python Simple HTTP Server Example terminal output Python SimpleHTTPServer Example Browser Directory Listing 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. Python SimpleHTTPServer

Python HTTP Server

Below image shows the terminal output for python http server module in python 3. Browser output remains same as in above images. python http server example 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

Security Considerations

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:

  • Data Exposure: The server exposes all files in the specified directory, including sensitive data. Ensure that you’re not serving sensitive information.
  • Access Control: The server does not provide any access control mechanisms. Anyone with network access can view your files.
  • SSL/TLS Support: The server does not support SSL/TLS encryption, making it vulnerable to eavesdropping and man-in-the-middle attacks.

To mitigate these risks, use the server only for local development and testing, and ensure that you’re not serving sensitive data.

Use Cases & Best Practices

Here are some use cases and best practices for using Python’s http.server:

  • Local Development: Use the server for local development and testing of web applications.
  • File Sharing: Use the server to share files with colleagues or friends over a local network.
  • Quick Prototyping: Use the server for quick prototyping and testing of web ideas.
  • Best Practices:
    • Use a non-root user to run the server.
    • Ensure that the server is not exposed to the public internet.
    • Use a firewall to restrict access to the server.
    • Regularly update your Python version to ensure you have the latest security patches.

Spinning up a local dev server to test front-end changes

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.

Serving static files during hackathons or project demos

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.

Using on Raspberry Pi to serve files over LAN

To use Python’s http.server on a Raspberry Pi to serve files over a LAN, follow these steps:

  • Install Python on your Raspberry Pi if it’s not already installed.
  • Navigate to the directory containing the files you want to serve.
  • Run the following command to start the server:
python -m http.server 8000
  • On another device connected to the same network, open a web browser and navigate to http://<Raspberry Pi's IP address>:8000.

Quick testing for webhooks or CORS headers

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.

FAQs

1. What is the command to start a simple HTTP server in Python 3?

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.

2. How can I specify a custom port?

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

3. How do I serve a different directory?

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.

4. Is Python’s http.server safe for production?

No, Python’s http.server is not recommended for production use due to security concerns. It’s designed for development and testing purposes only.

5. How do I serve over the network, not just localhost?

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.

Conclusion

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.

About the author(s)

Anish Singh Walia
Anish Singh WaliaSr Technical Writer
See author profile
Category:
Tutorial
Tags:

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 4, 2021

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

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.