Monday, 7 October 2013

Host a Simple Server and Share Files through LAN


If you need a quick web server running and you don't want to mess with setting up apache or something similar, then Python can help. Python comes with a simple builtin HTTP server. With the help of this little HTTP server you can turn any directory in your system into your web server directory. The only thing you need to have installed is Python.

All Thanks to Abhishek Damodara for introducing me to this powerful python command. I came across this when we both where working on a sudoku solver python code in Bhilai (yes PS-1 in Bhilai is EPIC)  and we needed to share the code. I was irritated with sharing code with pendrive so he just went to his working directory and wrote this simple script. All i had to do was just get his IP and open browser and type 10.X.X.XX:8000 and vola i can access all those file.

Assume that I would like to share the directory /home/arnab and my IP address is 10.3.1.69


Open up a terminal and type:
$ cd /home/arnab
$ python -m SimpleHTTPServer


That's it! Now your http server will start in port 8000. You will get the message:

Serving HTTP on 0.0.0.0 port 8000 ...





Now open a browser and type the following address:
http://10.3.1.69:8000

You can also access it via:
http://127.0.0.1:8000






If you wish to change the port that's used start the program via:
$ python -m SimpleHTTPServer 8080




To Stop the Server just press Ctrl + C .  Thats it you are done 

If u press Ctrl + Z instead u can start the server again u need to kill the process .To do so type:


$ netstat -plntu | grep 8000
$ kill -9 4920

8000 is the port where you hosted the server and  4920 is the process id which u got from the previous command's output.


Here i stopped the server by pressing Ctrl + Z , thats why i can't start the server again. I killed the process as shown.



No comments:

Post a Comment