代码501,消息在python中创建http服务器时不支持的方法('GET')

时间:2018-02-01 04:57:52

标签: python-3.x httpserver simplehttpserver

我试图在python中创建一个简单的http服务器。

以下是我写的代码:

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

当我尝试运行上面的python文件并转到http://localhost/hello.html时,我收到以下消息:

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我能够找到问题。我班上的方法应该是do_GET而不是do_get