SoFunction
Updated on 2024-11-12

Super Simple Python HTTP Service

Super if you need a simple Web Server urgently, but you do not want to go to download and install those complex HTTP service program, such as: Apache, ISS, etc.. Then Python may help you. A simple built-in HTTP server can be accomplished using Python. So, you can present all your directories and files as HTTP. The only thing you need to do is to install a Python.

In fact, this is a very useful way to share files. Implementing a miniature HTTP service program is a simple matter and requires only one command line in Python. Here is the command line: (assuming we need to share our directory /home/haoel and the IP address is 192.168.1.1)

$ cd /home/haoel
$ python -m SimpleHTTPServer

That will do, and our HTTP service is listening on port 8000. You'll get the following message:

Serving HTTP on 0.0.0.0 port 8000 ...

You can open your browser (IE or Firefox) and enter the URL below:

http://192.168.1.1:8000

If you have a file in your directory with the filename called , then that file will be a default page, and if there is no such file, then the directory listing will be shown.

If you want to change the port number, you can use the following command:

$ python -m SimpleHTTPServer 8080

If you only want this HTTP server to serve your local environment, then, you need to customize your Python's program a bit, here is an example:

import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = 
Protocol   = "HTTP/1.0"
if [1:]:
  port = int([1])
else:
  port = 8000
server_address = ('127.0.0.1', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = ()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()

Note: All of these things can be found in the Windows orCygwin Work under.

summarize

The above is a small introduction to the ultra-simple Python HTTP service, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. Here also thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!