Home / Web Engineering / Web Programming / Running a simple http server

Running a simple http server

Print Friendly

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

In Python 3.x

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!

comments

About Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.