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

标签 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 -

我做错了什么?

最佳答案

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

关于python-3.x - 在 python 中创建 http 服务器时代码 501,消息不支持的方法 ('GET' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48555628/

相关文章:

python - 在python中将一些数组合并为单个数组/列表

html - 在 HTML 文件中导入 Controller

python - 使用 Python 发送和解析 HTTP POST 请求

python - 使用 HTTPServer 时如何映射传入的 "path"请求?

java - JDK6 HTTP 服务器上带有 Spring 3 的 REST 服务

python - 如何从 socket.timeout 异常处理程序中捕获 KeyboardInterrupt?

python - pydrive如何通过id/title读取文件

python - 获取遗传算法中使用的对象的值

python - 使 python SimpleHTTPServer 在顶部列出子目录

Python SimpleHTTPServer 更改服务目录