Python HTTP service quick Building

Source: Internet
Author: User

Do you know? Using Python, you can quickly build an HTTP server to implement a micro Python HTTP service program! The following describes how to implement the Python HTTP service.

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)

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

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

 
 
  1. Serving HTTP on 0.0.0.0 port 8000 ... 

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  
  2. import BaseHTTPServer  
  3. from SimpleHTTPServer import SimpleHTTPRequestHandler  
  4. HandlerClass = SimpleHTTPRequestHandler  
  5. ServerClass  = BaseHTTPServer.HTTPServer  
  6. Protocol     = "HTTP/1.0" 
  7.  
  8. if sys.argv[1:]:  
  9.     port = int(sys.argv[1])  
  10. else:  
  11.     port = 8000 
  12. server_address = ('127.0.0.1', port)  
  13.  
  14. HandlerClass.protocol_version = Protocol  
  15. httpd = ServerClass(server_address, HandlerClass)  
  16.  
  17. sa = httpd.socket.getsockname()  
  18. print "Serving HTTP on", sa[0], "port", sa[1], "..." 
  19. httpd.serve_forever()  

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

  1. Analysis of Python 3.1 core language changes
  2. PHP veteran talks about the basic features of PHP and Python
  3. Comparison of Ruby and Python syntax
  4. Ruby usage tips: find efficient implementations
  5. Dialogue with the father of Python: the path to success in Python

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.