I'm trying to make a server programer. and the code look like this:
class ALNHTTPRequestHandler(BaseHTTPRequestHandler):
prefix = r'/Users/huxx/PycharmProjects/ServerDemo'
# handle GET command
def do_GET(self):
rootdir = ALNHTTPRequestHandler.prefix # file location
try:
if self.path.endswith('.html'):
finalPath = rootdir + self.path
with open(finalPath, 'rb') as f:
print('open successed')
# send code 200 response
self.send_response(200)
# send header first
self.send_header('Content-type', 'text-html')
self.end_headers()
# send file content to client
a = f.read()
self.wfile.write(a)
# self.wfile.write(f.read())
return
except IOError:
print('well not found')
self.send_error(404, 'file not foundbbbb')
def run():
print('http server is starting...')
server_address = ('127.0.0.1', 8011)
httpd = HTTPServer(server_address,ALNHTTPRequestHandler)
print('http server is running...')
httpd.serve_forever()
if __name__ == '__main__':
run()
the problem is if I use self.wfile.write(f.read()) rather than self.wfile.write(a), there's no response at all. Why is that?