Setup a Simple and Quick Web Server Using Python !!


Setup a Simple and Quick Web Server Using Python ( Python Tutorials: 6 )

Today we are going to talk about simple yet useful python library called SimpleHTTPServer to setup a simple and quick Web Server using Python. This might not be that interesting to some of the expert programmers but for beginners, this will definitely be an interesting article 😉 .

So let’s talk about SimpleHTTPServer now.

Basically, it’s a module in python and what it does is it makes a quick web server with which many users on a network can share files and do lots of other stuff. This module defines class SimpleHTTPServer.SimpleHTTPRequestHandler(request, client_address, server) which serves files normally from the current directory that you’re in. You can learn about classes defined by SimpleHTTPRequestHandler from the official documentation here.

Here is a simple demonstration of this module to set up a simple web server and access current directory files.

Save the above python script with the .py extension, and provide execution permission with chmod +x and finally execute it with ./ Basically, you won’t see any output in the terminal as for now.

There are other ways to accomplish this too. You can simply type the following command in the bash shell and get the same result.

Both are going to display the same current directory files or if you have a Html page in that directory then it will scan the directory and only display the Html file as the result. If you want to display other directory files as the output without manually navigating from the displayed output then just change directory into the preferred directory and execute the same provided command. Example:

This command will setup a web server in port 1337 and shows everything that’s inside ~/lincoder directory rather than showing root directory files as the default output.

Now with that said let’s check the output that this little local web server provides. Checking output for all the ways provided are similar, just follow along:

  • First and foremost, run the above script or command.
  • To access the web server, you need to know the ip address of the server system.
  • To find out the ip address, simply type ifconfig from Linux machine and ipconfig if you’re in Windows.
  • Now open up your favourite web browser and type the ip address of the web server along with the port number separated by a colon. Example:

  • After typing that in your browser hit enter and you will then be presented with all the directories and files present in that current directory.

Setup a Simple and Quick Web Server Using Python

Once you see the output in your browser try accessing the directories and files as you would normally do. After that go back to your server system and you should be able to see all the details about what you were doing while accessing the server. Also, you can change the port number (port) to whatever you want and use that port number while accessing the server.

Setup a Simple and Quick Web Server Using Python

Here are few interesting things you can do using this web server, these examples are just to make beginners get excited about programming and know the power of a simple code or program, which will definitely make all newbies to learn more and find more interesting things and start programming. You should enjoy while you code and get excited for the output because that’s the only way to start learning the real programming effectively. Always try to do interesting stuff and reveal the power of coding. So, get your computers out of your bag and start coding 😉 . Here are few things you can do to make use of this module:

  • Once you run your server, everybody on the network can access your web server if they have the IP address and the port number. So what you can do is change the directory to which you want to share the data and let others access it without doing hectic works like copying to pen drive or transferring through any means. This way you don’t have to give your data to anyone, just share with them and let them see through your little server.
  • You can even share your music library as well as movies and let your friends or brother/sister enjoy it while you’re working, no need to give the copy of files at all. Amazing right?!! 😀 .

I might be wrong somewhere while explaining, please correct me by mentioning it in the comments below 🙂 .

That’s pretty much it in this article, see you guys in the next one! keep pythoning 🙂 .