Tutorial

Python SimpleHTTPServer - Python HTTP Server

Published on August 3, 2022
Default avatar

By Pankaj

Python SimpleHTTPServer - Python HTTP Server

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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 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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

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

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

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

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

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

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel