Jean Pierre
Jean Pierre

Reputation: 211

http.server not serving pages as per example from python.org

I'm trying to start a simple dir server on a local network But I am getting this error

Error response

Error code: 501

Message: Unsupported method ('GET').

Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.

This is the example given at https://docs.python.org/3/library/http.server.html If I run it from the command line it works python3 -m http.server. I need to control this server over time so I need to turn it on for a while and turn it off automatically

from http.server import BaseHTTPRequestHandler, HTTPServer




def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
    server_address = ('0.0.0.0', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

Upvotes: 4

Views: 8183

Answers (1)

Leon
Leon

Reputation: 32484

The answer is in the documentation that you linked to:

The HTTPServer must be given a RequestHandlerClass on instantiation, of which this module provides three different variants:

class http.server.BaseHTTPRequestHandler(request, client_address, server)

This class is used to handle the HTTP requests that arrive at the server. By itself, it cannot respond to any actual HTTP requests; it must be subclassed to handle each request method (e.g. GET or POST). ...

For your case you should use http.server.SimpleHTTPRequestHandler instead:

class http.server.SimpleHTTPRequestHandler(request, client_address, server)

This class serves files from the current directory and below, directly mapping the directory structure to HTTP requests.

Upvotes: 2

Related Questions