Python http server directory

Nicolas Mesa

Serve Your Current Directory With Python and HTTP

Mon Sep 03, 2018 by Nicolas Mesa in command line, python, HTTP, tips

This is going to be a short post showing how to run an HTTP server to serve your current working directory.

TLDR

python -m SimpleHTTPServer 
alias serve="python3 -m http.server" 

Explanation

Sometimes at work, I’ve had the need to spin up a quick HTTP server to serve my current working directory. I usually need this for two use cases:

  1. Share a quick link to have people download something from my computer.
  2. I have an index.html file in another computer along with a bunch of static resources and don’t feel like copying the whole folder to be able to see the page in my browser. This use case happens a lot when I’m running unit tests on another computer and need to look at coverage reports (which are rendered in HTML).

It turns out that serving your current directory with an HTTP server is easy. You only need python .

Setup

Before we get started, let’s create some sample files.

nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file1 > file1.txt nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file2 > file2.txt nmesa@desktop-nicolas:~/demos/serve-cwd$ mkdir dir1 dir2 nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file3 > dir1/file3.txt nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file4 > dir2/file4.txt nmesa@desktop-nicolas:~/demos/serve-cwd$ tree . ├── dir1 │ └── file3.txt ├── dir2 │ └── file4.txt ├── file1.txt └── file2.txt 2 directories, 4 files

We created two files and two directories in the root, and one file in each of the directories.

Читайте также:  Php запуск сервера ubuntu

Find out your python version

The command to spin up the HTTP server varies depending on the version of python that you have. Run python —version to get your current version of python .

nmesa@desktop-nicolas:~/demos/serve-cwd$ python --version Python 3.5.2

If you see Python 3.x.x , use the Python 3 command. If you see Python 2.x.x , use the Python 2 command.

In this case, I’m using Python 3.

Python 3 command

nmesa@desktop-nicolas:~/demos/serve-cwd$ python3 -m http.server Serving HTTP on 0.0.0.0 port 8000 . 

Python 2 command

nmesa@desktop-nicolas:~/demos/serve-cwd$ python -m SimpleHTTPServer Serving HTTP on 0.0.0.0 port 8000 . 

Test it out

The previous commands spun up an HTTP server listening on port 8000 . Let’s point our web browser to the server’s IP address on port 8000 ( 192.168.0.250:8000 in this case). The page should look like this:

Root directory

Sample root directory listing

Sample root directory listing. This is the equivalent of running an ls on the directory where the server is running.

Child directory

dir1 directory listing

dir1 directory listing.

File

Let’s click the file3.txt link.

file3.txt contents

file3.txt contents. Note that this file rendered in the browser. This is because the HTTP server is smart enough to add a content type to the HTTP response. This becomes handy when you need to serve HTML along with CSS, JS, and images.

Update (Oct 21, 2018)

I’ve been using this a lot at work to serve test coverage reports. To save some typing, let’s add the following alias to our .bashrc ( .bash_profile in Mac) file:

alias serve="python3 -m http.server" 

Let’s test it out by running serve from our target directory (note that you will need to run source ~/.bashrc or start a new terminal for this to work):

nmesa@desktop-nicolas:~/demos/serve-cwd$ serve Serving HTTP on 0.0.0.0 port 8000 . 

We can also specify a different port:

nmesa@desktop-nicolas:~/demos/serve-cwd$ serve 9000 Serving HTTP on 0.0.0.0 port 9000 . 

This command starts the web server in port 9000 .

Conclusion

Python makes it easy to spin up an HTTP server to share your current working directory. Please don’t forget to kill the server once you’re done and be careful with what you share.

Share

About

Nicolas is a software engineer trying to learn how to talk to computers

Recent Posts

Источник

Python SimpleHTTPServer — Python HTTP Server

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.

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

python http server example

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. Learn more about us

Источник

Оцените статью