This article mainly introduces the use of Python to quickly build HTTP services and file sharing services. It has certain reference value. Now I share it with you. Friends in need can refer to it
SimpleHTTPServer
SimpleHTTPServer is an HTTP service class that comes with Python. By using it, we can quickly build an HTTP service and shared service on any platform (Window, Linux, MacOS). You only need to install the Python environment
How to use
If you want to start a file sharing service running on a specific port, you can execute The following command
python -m SimpleHTTPServer [port]
will display the files and directories in the current directory. If we do not specify the port, the default is 8000
Of course, the above command also opens an HTTP service. Assume that there is a file test in the current running directory, then we can request like this
curl "http://localhost:8000/test" -v
The request result is in test Content
Purpose
The software development process is a multi-person collaboration. When we develop a project that relies on other people’s HTTP interfaces , we can use SimpleHTTPServer to mock dependent interfaces, which can speed up joint debugging and debug program problems as early as possible. Generally, we use json strings in our files to simulate the results
Insufficiency
##The SimpleHTTPServer that comes with the python system only supports the GET and HEAD methods, not Supports POST method (test environment is python 2.7.10), simple modifications are required#部分源码 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()
def do_POST(self): """Serve a POST request.""" self.do_GET()
Use Python to monitor file content changes code
The above is the detailed content of Use Python to quickly build HTTP services and file sharing services. For more information, please follow other related articles on the PHP Chinese website!