不能 运行 Python3 ARM 处理器上的 HTTPServer

Cannot run Python3 HTTPServer on ARM processor

我最近在 Python3 买了 Odroid XU4, a single-board computer with an ARM CPU. I try to run a simple web server using HTTTPServer

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

这段代码 运行 在我的 Mac 机器上运行良好。但是当我在 Odroid XU4 上尝试 运行 时,我收到了这条错误消息。

$ python3 webserver.py
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    with socketserver.TCPServer(("", PORT), Handler) as httpd:
AttributeError: __exit__

谁能解释为什么我会收到这个错误?为了您的信息,我附上了有关 OS 和 Python 解释器的信息。

$ uname -a
Linux odroid 4.9.44-54 #1 SMP PREEMPT Sun Aug 20 20:24:08 UTC 2017 armv7l armv7l armv7l GNU/Linu

$ python 
Python 3.5.2 (default, Aug 18 2017, 17:48:00)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

the documentation 看来,TCPServer 的基础 class 的上下文管理器协议 (with ...) 形式(因此添加了 TCPServer在 python3.6 中。这在 python3.5

中不可用

Changed in version 3.6: Support for the context manager protocol was added. Exiting the context manager is equivalent to calling server_close().

幸运的是,您可以使用之前的方法。这大致意味着将你的 with 语句变成一个简单的赋值:

httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()