Python built-in HTTP protocol Server Simplehttpserver usage guide

Source: Internet
Author: User
First make sure Python is installed, I have the 2.x version, yes, my operating system is WIN7, in fact, for Python, what operating system is not important. Python has a simple HTTP server built-in, with just one command line underneath it, and an HTTP server up:

Python-m Simplehttpserver 80

The following 80 ports are optional and do not fill with the default port of 8000. Note that this will set the current folder as the default Web directory and try typing the native address in the browser:

Http://localhost:80
If the current folder has a index.html file, the file is displayed by default, otherwise all files under the directory are displayed as a list of files. This has achieved the most basic purpose of file sharing, you can make a script, and then create a shortcut, you can easily start sharing the file. If there are more needs, can be customized according to their own needs, specific see the Official document Simplehttpserver, or directly read the source code. I copy a paragraph for easy reference:

Import Simplehttpserverimport socketserverport = 8000Handler = SIMPLEHTTPSERVER.SIMPLEHTTPREQUESTHANDLERHTTPD = Socketserver.tcpserver (("", PORT), Handler) print "Serving at PORT", Porthttpd.serve_forever ()

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

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

Import sys import basehttpserver from simplehttpserver import Simplehttprequesthandler Handlerclass = Simplehttprequesthandler ServerClass = Basehttpserver.httpserver Protocol = "http/1.0"  if sys.argv[1:]:   port = Int (sys.argv[1]) Else:   Port = 8000 Server_address = (' 127.0.0.1 ', port)  handlerclass.protocol_version = Protocol httpd = ServerClass (server_address, Handlerclass)  

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

  • 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.