gpt4 book ai didi

python - 属性错误: module 'http.server' has no attribute 'ThreadingHTTPServer'

转载 作者:行者123 更新时间:2023-11-30 22:11:52 25 4
gpt4 key购买 nike

我正在尝试 this 上的代码Python3 关于 HTTP Server 的文档页面。

网站上发布的代码是:

def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()

此代码正在运行。我想尝试 ThreadingHTTPServer 所以,正如文档所说:

This class (ThreadingHTTPServer) is identical to HTTPServer but uses threads to handle requests by using the ThreadingMixIn. This is useful to handle web browsers pre-opening sockets, on which HTTPServer would wait indefinitely.

所以,我将上面的代码更改为:

def run(server_class=http.server.ThreadingHTTPServer, handler_class=http.server.BaseHTTPRequestHandler$
PORT = 8000
server_address = ('', PORT)
httpd = server_class(server_address, handler_class)
print("server running on port: ", PORT)
httpd.serve_forever()

但我收到以下错误:

Traceback (most recent call last):
File "simple_http_server.py", line 6, in <module>
def run(server_class=http.server.ThreadingHTTPServer, handler_class=http.server.BaseHTTPRequestHandler):
AttributeError: module 'http.server' has no attribute 'ThreadingHTTPServer'

我想补充一点,我最近才使用Python,所以我可能错过了一些东西。

您认为错误的原因是什么?我哪里做错了?

最佳答案

我想我知道你为什么会遇到这个问题。如果您仔细阅读:

class http.server.ThreadingHTTPServer(server_address, RequestHandlerClass)

This class is identical to HTTPServer but uses threads to handle requests by using the ThreadingMixIn. This is useful to handle web browsers pre-opening sockets, on which HTTPServer would wait indefinitely.

New in version 3.7. <-this

您可能没有最新版本。

关于python - 属性错误: module 'http.server' has no attribute 'ThreadingHTTPServer' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51300845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com