Use Python to quickly build HTTP services and file sharing services

不言
Release: 2018-06-04 16:00:26
Original
2021 people have browsed it

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]
Copy after login

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
Copy after login

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()
Copy after login

We can copy the SimpeHTTPServer method and implement it in it The do_POST method can

def do_POST(self):
 """Serve a POST request."""
 self.do_GET()
Copy after login

Save the file and run it to support POST request

Related recommendations:


Use Python to monitor file content changes code

##Use python to implement XlsxWriter to create and edit Excel files


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template