# 获取浏览器请求,并返回请求的文件内容
import socket
import re
import threading
import os
def handler_client(client_soc):
print('开始服务:', os.getpid())
# import time
# time.sleep(60)
# 获取请求头
request_head = client_soc.recv(1024).decode('utf-8')
# print(request_head)
if request_head: # 有的时候,浏览器传递的请求头是空的
# 解析要请求的文件 'GET /classic.css HTTP/1.1'
first_line = request_head.splitlines()[0]
path = re.match(r'[^/]+([^ ]+)', first_line).group(1) # 使用正则来获取请求的文件路径
print("获取到路径:",path)
# 如果路径是 / 则打开首页的 index.html
if path == '/':
path = '/index.html'
try:
# 读取文件内容作为响应体
file = open('html'+path, 'rb') # 拼接要请求的文件路径
resp_body = file.read()
file.close()
except:
# 发生异常,没有内容可以返回。返回错误 404
resp_head = 'HTTP/1.1 404 NOT FOUND\r\n'
resp_head += '\r\n'
resp_body = '文件不存在'
client_soc.send(resp_head.encode('utf-8'))
client_soc.send(resp_body.encode('utf-8'))
else:
# 正确读取到文件,返回响应数据
resp_head = 'HTTP/1.1 200 OK\r\n'
resp_head += '\r\n'
client_soc.send(resp_head.encode('utf-8'))
client_soc.send(resp_body)
# 关闭客户端套接字
client_soc.close()
print('服务结束:', os.getpid())
def main():
# 初始化服务器套接字
server_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 设置地址可以重用
server_soc.bind(("", 1315))
server_soc.listen(128)
while True:
# 获取客户端套接字
client_soc, client_addr = server_soc.accept()
# 处理客户端的数据收发
# handler_client(client_soc)
work = threading.Thread(target=handler_client, args=(client_soc,))
work.start()
# client_soc.close() # 子线程和主线程使用的是同一个套接字,只能关闭一次
# 关闭服务器套接字
server_soc.close()
if __name__ == '__main__':
main()
import socket
import re
import threading
import os
def handler_client(client_soc):
print('开始服务:', os.getpid())
# import time
# time.sleep(60)
# 获取请求头
request_head = client_soc.recv(1024).decode('utf-8')
# print(request_head)
if request_head: # 有的时候,浏览器传递的请求头是空的
# 解析要请求的文件 'GET /classic.css HTTP/1.1'
first_line = request_head.splitlines()[0]
path = re.match(r'[^/]+([^ ]+)', first_line).group(1) # 使用正则来获取请求的文件路径
print("获取到路径:",path)
# 如果路径是 / 则打开首页的 index.html
if path == '/':
path = '/index.html'
try:
# 读取文件内容作为响应体
file = open('html'+path, 'rb') # 拼接要请求的文件路径
resp_body = file.read()
file.close()
except:
# 发生异常,没有内容可以返回。返回错误 404
resp_head = 'HTTP/1.1 404 NOT FOUND\r\n'
resp_head += '\r\n'
resp_body = '文件不存在'
client_soc.send(resp_head.encode('utf-8'))
client_soc.send(resp_body.encode('utf-8'))
else:
# 正确读取到文件,返回响应数据
resp_head = 'HTTP/1.1 200 OK\r\n'
resp_head += '\r\n'
client_soc.send(resp_head.encode('utf-8'))
client_soc.send(resp_body)
# 关闭客户端套接字
client_soc.close()
print('服务结束:', os.getpid())
def main():
# 初始化服务器套接字
server_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 设置地址可以重用
server_soc.bind(("", 1315))
server_soc.listen(128)
while True:
# 获取客户端套接字
client_soc, client_addr = server_soc.accept()
# 处理客户端的数据收发
# handler_client(client_soc)
work = threading.Thread(target=handler_client, args=(client_soc,))
work.start()
# client_soc.close() # 子线程和主线程使用的是同一个套接字,只能关闭一次
# 关闭服务器套接字
server_soc.close()
if __name__ == '__main__':
main()