Web服务器的底层原理与实现
2023-02-26 12:27:59 时间
import socket, threading, time
from Http.HttpResponse import HttpResponse
from WebFrame.WebFrame import handler_request
class HttpWebServer(object):
(福利推荐:阿里云、腾讯云、华为云服务器最新限时优惠活动,云服务器1核2G仅88元/年、2核4G仅698元/3年,点击这里立即抢购>>>)
__run = True def __init__(self, ip_address:str,listen_port: int): self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set socket reuse -> True default:False self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) self.server_socket.bind( (ip_address, listen_port) ) self.server_socket.listen(128) @staticmethod def handler_http_request(new_socket: socket.socket): """ :param new_socket: :return: """ rec_data = new_socket.recv(4096) if len(rec_data) == 0: return content = rec_data.decode("utf-8") request_path = content.split(" ", maxsplit=2)[1] if request_path == '/': request_path = '/index.html?userCode=wrvvs1rm' if request_path.endswith(".html?userCode=wrvvs1rm"): """ ---------------- dynamic request ----------------""" http_response = handler_request( {"request_path": request_path} ) new_socket.send(http_response) else: """ ---------------- static request --------------- """ response_headers, response_body, response_length = None, None, 0 response_status = '' try: with open("../static" + request_path, 'rb') as f: response_body = f.read() response_length: int = len(response_body) response_status = HttpResponse.RESPONSE_STATUS_OK except BaseException as e: with open('../static/html/404.html?userCode=wrvvs1rm', 'rb') as f: response_body = f.read() response_length: int = len(response_body) response_status = HttpResponse.RESPONSE_STATUS_NOT_FOUND finally: local_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) response_headers = f'HTTP/1.1 {response_status}rnContent-Length:{response_length}rnDate:{local_time}rnServer: Pagchern' response = response_headers.encode("utf-8") + "rn".encode("utf-8") + response_body new_socket.send(response) new_socket.close() def start(self) -> None: print("Server Start Successful!") while self.__run: new_socket, address_info = self.server_socket.accept() print(f"[+] client {address_info[0]} send request") sub_thread = threading.Thread(target=HttpWebServer.handler_http_request, args=(new_socket,)) sub_thread.setDaemon(True) sub_thread.start() def stop(self) -> None: self.__run = False print("Server Stop Successful!")
if name == ‘__main__’:
server=HttpWebServer('',8888) server.start()
你还在原价购买阿里云、腾讯云、华为云、天翼云产品?那就亏大啦!现在申请成为四大品牌云厂商VIP用户,可以3折优惠价购买云服务器等云产品,并且可享四大云服务商产品终身VIP优惠价,还等什么?赶紧点击下面对应链接免费申请VIP客户吧:
相关文章
- Linux 下安装 Nginx
- 教你一招,解决Github图片不显示问题(2021.1.20测试可用)
- Linux 学习笔记
- Github下载release附件变相提速方法
- 流程图软件 drawio 免费 github开源
- Git多人协作
- Linux 下 Too many open files 问题排查与解决
- Linux 下安装 fail2ban
- Git 推送到多个远程仓库
- Github团队开发示例(二)
- Github团队开发示例(一)
- Github教程(3)
- Github教程(2)
- Github教程(1)
- Github教程(0)
- jenkins配置从git指定分支下载代码,打包部署jar包
- 一台电脑支持2个git账号:gitlab+github
- 资产收集神器-Goby工具
- phpStudy 2016搭建-upload-labs靶场
- Adobe Photoshop 2022(mac+windows全版本都有,包括最新的2023)