A very simple Python HTTP service

Source: Internet
Author: User

If you need a simple Web Server, but you don't want to download and install the complex HTTP service programs, such as: Apache,iss. Well, Python might help you. Use Python to complete a simple built-in HTTP server. As a result, you can display your directories and files in HTTP. Tiao only one thing to do, is to install a python.

In fact, this is a very useful way to share files. It is simple to implement a tiny HTTP service program, which requires only one command line under Python. Here's the command line: (assuming we need to share our directory /home/haoel and the IP address is 192.168.1.1)

12 $ cd/home/haoel$ python -m SimpleHTTPServer

That's fine, and our HTTP service listens on port No. 8000. You will get the following information:

Serving HTTP on 0.0.0.0 Port 8000 ...

You can open your browser (ie or Firefox) and enter the following URL:

http://192.168.1.1:8000

If your directory has a file name named index.html, then this file will become a default page, if there is no such file, then the directory list will be displayed.

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

1 $ python -m SimpleHTTPServer 8080

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

12345678910111213141516171819 import sysimport BaseHTTPServerfrom SimpleHTTPServer import SimpleHTTPRequestHandlerHandlerClass = SimpleHTTPRequestHandlerServerClass  = BaseHTTPServer.HTTPServerProtocol     = "HTTP/1.0"if sys.argv[1:]:    port = int(sys.argv[1])else:    port = 8000server_address = (‘127.0.0.1‘, port) HandlerClass.protocol_version = Protocolhttpd = ServerClass(server_address, HandlerClass)sa = httpd.socket.getsockname()print "Serving HTTP on", sa[0], "port", sa[1], "..."httpd.serve_forever()

Note: All of these things can work under Windows or Cygwin.

Original address: http://coolshell.cn/articles/1480.html

A very simple Python HTTP service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.