为什么我的 python Web 服务器不工作?

why does my pyhhont webserver doesnt work?

我想制作一个带日志记录的多线程 python 网络服务器 我写了这段代码,它没有错误,但它没有显示页面,也没有在日志文件中写入任何内容,我不知道哪一部分是错误的

from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from os import curdir,sep
port_number=8080
class myhandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path=="/":
            self.path="/a1.html" 
        try:
             sendrep=False
             if self.path.endswith(".html"):
                 sendrep-True
                 mimetype='text/html'
             if self.path.endswith(".jpg"):
                 sendrep-True  
                 mimetype='image/jpg' 
             if self.path.endswith(".gif"):
                 sendrep-True 
                 mimetype='image/jpg'
             if self.path.endswith(".js"):
                 sendrep-True 
                 mimetype='application/javascript'
             if self.path.endswith(".css"):
                 sendrep-True 
                 mimetype='text/css'
             if sendrep==True:
                f=open(curdir+sep+self.path)
                self.send_response(200)
                self.send_header('content-type',mimetype)
                self.end_headers()
                self.wfile.write(f.read())
                f.close()
             return
        except IOError:
            self.send_error(404,'file not found: %s' % self.path)
        log_file = open('logfile.txt', 'w')
        def log_message(self, format, *args):
            self.log_file.write("%s - - [%s] %s %s\n" %(self.client_address[0],self.log_date_time_string(),curdir+sep+self.path,format%args))
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
     """Handle requests in a separate thread."""
print("asb")
try:
     server=ThreadedHTTPServer(('127.0.0.1',8080),myhandler)
     server.serve_forever()
except KeyboardInterrupt:
if KeyboardInterrupt:
    print('shut down the server')
    server.socket.close()

两件事:

  1. 几行有sendrep-True,这不是语法错误,而是无用的语句。它应该是 sendrep = True(等于而不是负)。

  2. 您还以二进制模式打开文件:f=open(curdir+sep+self.path, "br") 否则您将得到 TypeError: a bytes-like object is required, not 'str'