Practically speaking this is very useful to share files inside your local network. We like to use it for sharing documentation. Implementing this tiny but hugely useful HTTP server is very simple, its just a single line command. Note: this server is NOT SECURE. Use at your own risk.
Assume that I would like to share the directory /home/pwsrvr (or ~/pwsrvr) and my IP address is 192.168.1.200
Open up a terminal and type:
$ cd /home/your_dir_2_b_used $ python -m SimpleHTTPServer or
c:\> cd /home/your_dir_2_b_used
c:\> python -m SimpleHTTPServer
That's it! Now your http server will start in port 8000. You will get the message:
$ python -m SimpleHTTPServer 8080 Serving HTTP on 0.0.0.0 port 8080 ...
Now open a browser and type the following address:
127.0.0.1 - - [28/Dec/2014 02:10:48] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [28/Dec/2014 02:10:48] "GET /ss.png HTTP/1.1" 200 -
127.0.0.1 - - [28/Dec/2014 02:10:48] "GET /cgi.gif HTTP/1.1" 200 -
127.0.0.1 - - [28/Dec/2014 02:10:48] "GET /ha.jpg HTTP/1.1" 200 -
You can also access it via:
If you want to use "localhost", make sure it is set up in your hosts file. Seen where it was not. If the directory has a file named index.html, that file will be served as the initial file. If there is no index.html, then the files in the directory will be listed.
1.gif cgi-bin ha.jpg ip.png ss.png wmfb.php
backup cgi.gif icon.ico lo thankyou1.php
bash1.png chmod.png index.html sl.png wmfb.dat
bashscript.html cron.png indexhtml.old sp.png wmfb.html
Index.html has been renamed to text. If you just want to serve files, this might be a better way to do it.
$ python -m SimpleHTTPServer 8080
For more security, if you want to only serve on localhost you will need to write a custom Python program such as: (untested)
import sys import BaseHTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler HandlerClass = SimpleHTTPRequestHandler ServerClass = BaseHTTPServer.HTTPServer Protocol = "HTTP/1.0" if sys.argv[1:]: port = int(sys.argv) else: port = 8000 server_address = ('127.0.0.1', port) HandlerClass.protocol_version = Protocol httpd = ServerClass(server_address, HandlerClass) sa = httpd.socket.getsockname() print "Serving HTTP on", sa, "port", sa, "..." httpd.serve_forever()
Note also that this should also work on MSWindows or MSWindows with Cygwin. Have not tried on an Apple Mac. you can find Python at https://www.python.org/downloads/