Running a simple http server

Print Friendly, PDF & Email

In the world of web engineering and development, developing web-based applications always need a web application server where all web pages are accommodated. Depending on what web technology as well as scripting language is used at server-side, commands to run a web server is various. For  example, there are some web servers such as Apache (currently known as Apache2), Nginx, NodeJS etc.

However, if you want to start instantly a web server regardless of mess settings up, Python could help you create a simple server based on builtin HTTP Server. This feature could turn any directory in your system into your web server directory.

Practically speaking this is very useful to share files inside your local network. Implementing this tiny but hugely useful HTTP server is very simple, its just a single line command.

Open up a terminal and type of the following code snippets:

In Python 2.x

cd /home/your_web_directory
python -m SimpleHTTPServer [port_number]

In Python 3.x

cd /home/your_web_directory
python -m http.server [port_number]

That’s it! Now your http server will start in port 8000  if [port_number] is not precise. You will get the messages looking like the following image:

A demonstration of http server in Python
A demonstration of http server in Python

Now open a browser and type one of the following addresses:

If you have a specific port indicating the running code snippet, replace 8000  with this port number.

If your directory is containing an index.html or default.html, you could see its content rendered on your web browser. Otherwise, your directory would be browsed. To stop the web server, you just need to hit Ctrl-C  from the keyboard.

Note also that this should also work on Windows or Cygwin.

This explanation was also matched with the discussions on Stackoverflow.

Thoughts and clarifications are welcome!

One thought on “Running a simple http server

Leave a Reply

Your email address will not be published. Required fields are marked *



This site uses Akismet to reduce spam. Learn how your comment data is processed.