本文介绍了无法在 ARM 处理器上运行 Python3 HTTPServer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近购买了 Odroid XU4,带有ARM CPU.我尝试在 Python3 上使用 HTTTPServer 运行一个简单的 Web 服务器.

I recently bought Odroid XU4, a single-board computer with an ARM CPU. I try to run a simple web server using HTTTPServer on Python3.

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 上运行它时,我收到此错误消息.

This code runs well on my Mac machine. But when I try to run this on Odroid XU4, I got this error message.

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

谁能解释为什么我收到这个错误?为了您的信息,我附上了有关操作系统和 Python 解释器的信息.

Can anyone explain why I got this error? For your information, I’ve attached the information about the OS and Python interpreter.

$ 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.
>>>

推荐答案

来自 文档 似乎 TCPServer 的基类的上下文管理器协议(with ...)形式(因此 TCPServer 是在python3.6中添加.python3.5中没有此功能

From the documentation it would seem that the contextmanager protocol (with ...) form for TCPServer's base class (and therefore TCPServer was added in python3.6. This is not available in python3.5

在 3.6 版更改:添加了对上下文管理器协议的支持.退出上下文管理器相当于调用 server_close().

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

Fortunately, you can use the previous approach. This roughly means taking your with statement and turning it into a plain assignment:

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

这篇关于无法在 ARM 处理器上运行 Python3 HTTPServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 00:48