【问题标题】:code 501, message Unsupported method ('GET') while creating http server in python代码 501,在 python 中创建 http 服务器时消息不受支持的方法 ('GET')
【发布时间】:2018-02-01 04:57:52
【问题描述】:

我试图在 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 -

我做错了什么?

【问题讨论】:

    标签: python-3.x httpserver simplehttpserver


    【解决方案1】:

    我能够找到问题所在。我的类里面的方法应该是do_GET而不是do_get

    【讨论】:

      猜你喜欢
      • 2019-05-05
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2021-06-17
      • 2021-12-23
      • 2014-08-26
      • 1970-01-01
      • 2014-12-16
      相关资源
      最近更新 更多