面向对象多任务静态web服务器开发报错 AttributeError: 'HttpWebServer' object has no attribute 'tcp_ser_sk'
能否帮忙看看哪里错了
import socket
import threading
class HttpWebServer(object):
def __int__(self):
tcp_ser_sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_ser_sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
tcp_ser_sk.bind(("", 8080))
tcp_ser_sk.listen(10)
self.tcp_ser_sk = tcp_ser_sk
# 由于handle_request方法并未用到self对象,也未用到当前类,所以采取静态方法
@staticmethod
def handle_request(new_socket, ip_port):
recv_data = new_socket.recv(4096)
if len(recv_data) == 0:
print(f'客户端{ip_port}已下线')
new_socket.close()
recv_content = recv_data.decode('utf-8')
print(recv_content)
request_line = recv_content.split(' ', maxsplit=2)
request_path = request_line[1]
print(request_path)
if request_path == '/':
request_path = '/index1.html'
try:
with open('C:/Users/XwX/Desktop/html' + request_path, 'rb') as file:
file_data = file.read()
except Exception as e:
with open('C:/Users/XwX/Desktop/html/erro404.html', 'rb') as file_erro:
file_data_erro = file_erro.read()
response_line = "HTTP/1.1 404 Not Found\n"
response_header = "Server: PWS/1.0\n"
response_body = file_data_erro
response = (response_line + response_header + '\n').encode('utf-8') + response_body
new_socket.send(response)
else:
response_line = "HTTP/1.1 200 OK\n"
response_header = "Server: PWS/1.0\n"
response_body = file_data
response = (response_line + response_header + '\n').encode('utf-8') + response_body
new_socket.send(response)
finally:
new_socket.close()
def start(self):
while True:
new_socket, ip_port = self.tcp_ser_sk.accept()
print("客户端已上线:", ip_port)
sub_threading = threading.Thread(target=self.handle_request, args=(new_socket, ip_port), daemon=True)
sub_threading.start()
def main():
web_server = HttpWebServer()
web_server.start()
if __name__ == '__main__':
main()