Python3 -m http.server 只能单线程吗?

Python3 -m http.server 只能单线程吗?

Python -m http.server 开启的服务好像只能是单线程访问?

一个文件再被下载的时候,其他人操作不了这个文件,请问下可以开启多线程吗?


回答:

这个只是给你临时用用的一个 http 服务器, 所以并不提供多线程功能
如果你确实需要, 少量代码自己扩展下就行了, 或者跑个 nginx 之类的也不是什么大事


回答:

Linux; Python3.6

$ python3.6 -m http.server

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

^C

Keyboard interrupt received, exiting.

$ cat /usr/lib/python3.6/http/server.py

...

def test(HandlerClass=BaseHTTPRequestHandler,

ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""):

"""Test the HTTP request handler class.

This runs an HTTP server on port 8000 (or the port argument).

"""

server_address = (bind, port)

...

if __name__ == '__main__':

parser = argparse.ArgumentParser()

...

if args.cgi:

handler_class = CGIHTTPRequestHandler

else:

handler_class = SimpleHTTPRequestHandler

test(HandlerClass=handler_class, port=args.port, bind=args.bind)

$

server.py

...

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

"""Simple HTTP request handler with GET and HEAD commands.

This serves files from the current directory and any of its

subdirectories. The MIME type for files is determined by

calling the .guess_type() method.

The GET and HEAD requests are identical except that the HEAD

request omits the actual contents of the file.

"""

server_version = "SimpleHTTP/" + __version__

def do_GET(self):

"""Serve a GET request."""

f = self.send_head()

if f:

try:

self.copyfile(f, self.wfile)

finally:

f.close()

...


看到了吗?
源码就没有 Thread(多线程) 或 fork (多进程) 的行为!

以上是 Python3 -m http.server 只能单线程吗? 的全部内容, 来源链接: utcz.com/a/156432.html

回到顶部