Use Python to quickly build HTTP services and file sharing services

Source: Internet
Author: User
This article mainly introduces the use of Python to quickly build HTTP services and file sharing services, has a certain reference value, now share to everyone, the need for friends can refer to

Simplehttpserver

Simplehttpserver is a Python-brought HTTP service class that allows us to quickly build an HTTP service and shared services on any platform (Window,linux,macos), simply by installing the Python environment

How to use

If you want to open a file sharing service that runs on a specific port, you can execute the following command

Python-m Simplehttpserver [Port]

This will show the files and directories in the current directory, if we do not specify a port, the default is 8000

Of course, the above command also opened an HTTP service, assuming that the current run directory by a file test, then we can request

Curl "Http://localhost:8000/test"-V

The result of the request is the contents of test

Use

The software development process is multi-person collaboration, when we in the development project depends on other people's HTTP interface, we can use the simplehttpserver to mock the dependent interface, this can speed up the speed of the connection, debugging the problem of the program early. In general our files use JSON strings to simulate the results

Insufficient

The python system comes with a simplehttpserver that only supports the get and head methods, does not support the Post method (test environment is Python 2.7.10) and requires simple modifications

#部分源码def Do_get (self): "" "Serve a GET request." "" F = self.send_head () If f:try:  self.copyfile (F, Self.wfile) finally :  f.close () def do_head (self): "" "Serve a HEAD request." "F = self.send_head () if F:f.close ()

We can make a copy of the Simpehttpserver method, and then implement the Do_post method inside

def do_post (self): "" "Serve a POST request." "Self.do_get ()

Post-request is supported when the file is saved and run

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.