This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12121|Reply: 0

[Source] Python uses http.server to open an http service

[Copy link]
Posted on 12/2/2020 3:55:03 PM | | | |
Requirements: Some third-party tools are python scripts, I do C# development myself, I am not proficient in python itself, if you translate python code into C# code, it is very difficult, it is better to use python to open an http service, I call interface two, and finally execute python itself to execute its internal code.

HTTPServer is a subclass of socketserver.TCPServer. It creates and listens for HTTP sockets to dispatch requests to handlers.

Documentation:The hyperlink login is visible.

Tip: http.serverNot recommended for production。 It only implements basic security checks.

The code is as follows:


Use Python to listen for port 10254, enable the HTTP service, and try to access the URL, as shown in the following figure:



The tests are accessible, however, when I use the edge browser to access multiple times, sometimes alwayspendingstate, after I establish a connection from the native through telnet and port 10254, I found that the http service is not accessible from both the server and the local, as shown in the picture below,No response for 20 minutes, and in the end, I could only re-request:


At first, I thought it was a problem that there was no "Content-Length" setting, causing the browser to not know how many bytes of content to read, and I didn't know when the reading was completed, but after the response header, the problem was still not solved.

Could it be that the default is single-threaded, allowing only one client to access at a time?

Looking at the documentation, I found that there is a ThreadingHTTPServer class, which uses threads to process requests by using threads, and the code is changed to the following:

Problem solved, finished.






Previous:Use JSC to generate a dll in JavaScript for C# calls
Next:python uses mirror stations to speed up pip downloads
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com