Summary of implementing the Python HTTP service method

Source: Internet
Author: User

The following describes in detail how to use Python to quickly build an HTTP server and implement a micro-Python HTTP service program. What is the implementation method of the Python HTTP service, so please read this article carefully.

If you urgently need a simple Web Server, but you do not want to download and install complex HTTP service programs, such as Apache and ISS. Then, Python may help you. Python can be used to complete a simple built-in HTTP server. Therefore, you can display your directories and files in HTTP mode. Compile only needs to do one thing, that is, install a Python.

In fact, this is a very useful way to share files. It is very easy to implement a micro HTTP service program. In Python, you only need a command line. Below is the command line: Suppose we need to share our directory/home/haoel and the IP address is 192.168.1.1 ).

This works, and our HTTP service listens on port 8000. You will get the following information:

 
 
  1. $ cd /home/haoel  $ python -m SimpleHTTPServer   

You can open your Browser IE or Firefox) and enter the following URL:

 
 
  1. http://192.168.1.1:8000  

If there is a file named index.html in your directory, the file will become a development page. If there is no such file, the directory list will be displayed. To change the port number, run the following command:

 
 
  1. $ python -m SimpleHTTPServer 8080  

If you only want this HTTP server to serve the local environment, you need to customize your Python program. The following is an example:

 
 
  1. import sys  import BaseHTTPServer  from SimpleHTTPServer import SimpleHTTPRequestHandler  
  2. HandlerClass = SimpleHTTPRequestHandler  ServerClass  = BaseHTTPServer.HTTPServer 
  3.  Protocol     = "HTTP/1.0"  
  4. if sys.argv[1:]:      port = int(sys.argv[1]) 
  5.  else:      port = 8000 server_address = ('127.0.0.1', port)   
  6. HandlerClass.protocol_version = Protocol  httpd = ServerClass(server_address, HandlerClass) 
  7.   sa = httpd.socket.getsockname()  print "Serving HTTP on", sa[0], "port", sa[1], "..." httpd.serve_forever()   

This completes our Python HTTP service. Note: all these things can work in Windows or Cygwin.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and encoding solution
  5. Analysis of Python GIL and thread security

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.