Python builds a simple HTTP service (3.x version and 2.x version) __python

Source: Internet
Author: User

Original Reprint Address: http://www.cnblogs.com/91allan/p/4889167.html


Needless to say, we often work with the need for HTTP services, if we do not want to engage the complex Apache, IIS servers, and so on, we can use Python to help us build a simple server. The operation is as follows:

1, download and install a python;

2, then set the environment variable, in the system properties of the system variable, double-click Path, add after the value of the variable; C:\Python27 (Take python27 as an example, if you are python26, change the number on the back). No, look at the link below:

http://jingyan.baidu.com/article/48206aeafdcf2a216ad6b316.html;

3, open the cmd command, enter the file path to read:

CD C:\Users\allan\Desktop\ new Folder

(CD to read XX, followed by your file path), such as I want to open a new folder in the index.html as an example

4,

1 python2.x built-in a simple HTTP server, only need to hit a line under the command Line command, an HTTP server is up:

Python-m simplehttpserver 8080

2 python3.x built-in a simple HTTP server, only need to hit a line under the command Line command, an HTTP server is up:

Python-m http.server 8080

The 8080 ports that follow are optional and the default port 8000 is not filled in. Note that this will set the current folder as the default Web directory, and try typing the native address in the browser:

http://localhost:8080

If the current folder has a index.html file, the file is displayed by default, or all files in the directory are displayed as a list of files. This has achieved the most basic file sharing purposes, you can make a script, and then create a shortcut, you can easily start to share the file. If there is more demand, can be customized according to their own needs, specific see the Official document Simplehttpserver, or directly look at the source code. Copy a paragraph for easy reference:

Import simplehttpserver
import socketserver

PORT = 8000

Handler = Simplehttpserver.simplehttprequesthandler

httpd = Socketserver.tcpserver ((", PORT), Handler)

print" Serving at Port ", Port
Httpd.serve_forever ()


Over.

Report:

If you need a mobile phone display: With the computer with WiFi under the browser input your computer IP address + filename:

192.168.0.101:8000/index.html

For the first time, please correct me if there are any flaws. Thank you.


Original Reprint Address: http://www.cnblogs.com/91allan/p/4889167.html

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.