MoonPoint Support Logo


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals

Advanced Search
Sun Mon Tue Wed Thu Fri Sat

Tue, Jun 07, 2016 11:59 pm

Using Python for a simple web server

On a Mac OS X system, you can use Python to start a simple web server in any directory by opening a Terminal window (the Terminal program is found in Applications/Utilities) and entering the command python -m SimpleHTTPServer port with port being the port you wish to access the web server on. Typically, web servers listen on TCP port 80 for HTTP connections, but you can specify any currently unused port on the system with the caveat that if you pick a well-known port, i.e., a port less than 1,024, then you will need to prefix the command with sudo to run the command as root, .e.g, sudo python -m SimpleHTTPServer 80. However, you can pick ports above 1,023, such as 8080, without using sudo. E.g., python -m SimpleHTTPServer 8080. If you issued that command, you should see Python respond with "Serving HTTP on port 8080 ..." Then, within a browser, you could access an HTML file in the directory from which you issued the command with http://localhost:8080/filename. E.g., if I wished to display a file named test.html, I could use http://localhost:8080/test.html. If test.html was just a simple HTML file, I would see GET /test.html HTTP/1.1" 200 displayed on the command line where the Python command was issued. The "200" at the end of the line is an HTTP status code indicating a successful HTTP request.

If you have a file named index.html in the directory, then just as with a normal webserver, you don't need to specify it to view its contents. E.g. http://localhost:8080 would be sufficient for displaying index.html. If there are links to images within index.html, Python would display the relevant HTTP GET commands the browser issued to retrieve the images.

When you wish to stop Python from listening on the specified port, you can use the control-C keys to stop the web server and return to the shell prompt. Note: you will see some error messages displayed when you do so.

Below is the output displayed by Python when I first accessed a file named test.html and then when I accessed the index.html file in the same directory. The index.html file had links to 4 image files contained in the same directory as the index.html file, so the GET commands issued by the browser are displayed for those as well. The IP address is the localhost address. The last line shown was produced when I hit the control and "C" keys.

$ python -m SimpleHTTPServer 8080 Serving
HTTP on port 8080 ... - - [08/Jun/2016 00:00:14] "GET /test.html HTTP/1.1" 200 - - - [08/Jun/2016 00:00:32] "GET / HTTP/1.1" 200 - - - [08/Jun/2016 00:00:32] "GET /vlc_dmg.png HTTP/1.1" 200 - - - [08/Jun/2016 00:00:32] "GET /vlc_internet_download.png HTTP/1.1" 200 - - - [08/Jun/2016 00:00:32] "GET /check_album_art.png HTTP/1.1" 200 - - - [08/Jun/2016 00:00:32] "GET /vlc_media_player.png HTTP/1.1" 200 - - - [08/Jun/2016 00:00:32] "GET /jamendo_selections.png HTTP/1.1" 200 - 

[/languages/python] permanent link

Once You Know, You Newegg AliExpress by

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo