import socket
import re
def request_service(new_socket):
"""为这个客户端返回数据"""
#接受浏览器发过来的请求,即http请求
recv_data = new_socket.recv(1024).decode("utf-8")
request_lines = recv_data.splitlines()
#if len(request_lines) == 0:
#request_service(new_socket)
file_name = ""
#正则表达式
ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
if ret:
file_name = ret.group(1)
if file_name == "/":
file_name = "/index.html"
print("@"*45)
print(file_name)
#返回http格式的数据,给浏览器
try:
f = open("./html" + file_name, "rb")
except :
response = "HTTP/1.1 404 NOT FOUND\r\n"
response += "\r\n"
response += "-----file not found-----"
new_socket.send(response.encode("utf-8"))
else:
html_content = f.read()
f.close()
#准备发送给浏览器的数据---header
response = "HTTP/1.1 200 OK\r\n\r\n"
#将response header发给浏览器
new_socket.send(response.encode("