python3实现一个http代理服务器代码
代码语言:python
所属分类:其他
代码描述:python3实现一个http代理服务器代码,支持https及http
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
# encoding:utf-8 import socket import _thread class Header: """ 用于读取和解析头信息 """ def __init__(self, conn): self._method = None header = b'' try: while 1: data = conn.recv(4096) header = b"%s%s" % (header, data) if header.endswith(b'\r\n\r\n') or (not data): break except: pass self._header = header self.header_list = header.split(b'\r\n') self._host = None self._port = None def get_method(self): """ 获取请求方式 :return: """ if self._method is None: self._method = self._header[:self._header.index(b' ')] return self._method def get_host_info(self): """ 获取目标主机的ip和端口 :return: """ if self._host is None: method = self.get_method() line = self.header_list[0].decode('utf8') if method == b"CONNECT": host = line.split(' ')[1] if ':' in host: host, port = host.split(':') else: port = 443 else: for i in self.header_list: if i.startswith(b"Host:"): host = i.split(b" ") if len(host) < 2: continue host = host[1].decode('utf8') break else: host = line.split('/')[2] if ':' in host: host, port = host.split(':') else: port = 80 self._host = host self._port = int(port) return self._host, self._port @property def data(self): """ 返回头部数据 .........完整代码请登录后点击上方下载按钮下载查看
网友评论0