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