我试图用 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/