Search code examples
python-3.xhttpserversimplehttpserver

code 501, message Unsupported method ('GET') while creating http server in python


I was trying to create a simple http server in python.

The following is the code that I wrote:

from http.server import BaseHTTPRequestHandler, HTTPServer
from os import curdir, sep

PORT_NUMBER = 8080


class MyHandler(BaseHTTPRequestHandler):
    def do_Get(self):

        print(self.path)
        value = ''
        send_reply = False
        if self.path.endswith(".html"):
            send_reply = True
            value = "text/html"

        if send_reply:
            f = open(curdir + sep + self.path)
            self.send_response(200)
            self.send_header('Content type', value)
            self.end_headers()
            self.wfile.write(f.read())
            f.close()
        else:
            self.send_error(404, "File not Found")
        return


try:
    server = HTTPServer(('', PORT_NUMBER), MyHandler)
    print("Server started")
    server.serve_forever()

except Exception as e:
    print(e)
    server.socket.close()

When I try to run the above python file and go to http://localhost/hello.html, I get the following message:

code 501, message Unsupported method ('GET')
"GET /favicon.ico HTTP/1.1" 501 -

What am I doing wrong?


Solution

  • I was able to find the issue. The method inside my class should be do_GET instead of do_get