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

标签 python python-3.x httpserver

我正在尝试 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/

相关文章:

python - 正则表达式 - 通过改变中间值从 HTML 中获取值(value)

python - One-hot 编码的逻辑回归

Python tensorflow 精简版错误 :Cannot set tensor: Got tensor of type 1 but expected type 3 for input 88

python - 导入模块并使用其方法

javascript - 两个 node.js 服务器?

python - 基于列的 2 个数据框之间的减法

Python 3 单元测试无法模拟函数内部使用的方法

python - 如何增加 Paramiko SSH 客户端上的向后滚动缓冲区?

node.js - Node http 服务器无法在 Ubuntu Linux 上运行

java - com.sun.net.httpserver 报错,是什么意思?