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