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.
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
Below 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
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.