Im trying the code on this documentation page of Python3 about HTTP Server.
The code posted on the site is:
def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
this code is working. I would to try the ThreadingHTTPServer so, as the documentation says:
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.
So, I changed the code above to:
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()
but I get following error:
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'
I would like to add that it is only recently that I use Python, so I may have missed something.
What do you think is the cause of the error? Where am I doing wrong?
I think I know why you got this issue. If you read carefully:
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
It is possible you don't have the newest version.