pythonpython-3.xarmodroid

Cannot run Python3 HTTPServer on ARM processor


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()

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__

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

Solution

  • 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

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

    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()