Python入门:python实现http透明代理
2016-08-23 15:22
483 查看
#-*- coding: UTF-8 -*- import socket,select import sys import threading from multiprocessing import Process class Proxy: def __init__(self,soc): self.client,_=soc.accept() self.target=None self.request_url=None self.BUFSIZE=4096 self.method=None self.targetHost=None def getClientRequest(self): request=self.client.recv(self.BUFSIZE).decode() if not request: return None cn=request.find('\n') firstLine=request[:cn] print(firstLine[:len(firstLine)-9]) line=firstLine.split() self.method=line[0] self.targetHost=line[1] return request def getHeader(self,request): cn = request.find('\n') done = request[cn + 1:] #print(done) headers = {} print('!!!!!!!!!!!!!!!') for i in done.split('\r\n\r\n')[0].split('\r\n'): #print(i) if i != '': k, v = i.split(': ') headers[k] = v print(headers) data = {} for i in done.split('\r\n\r\n')[1].split('&'): if i != '': k,v = i.split('=') data[k] = v print(data) return [headers,data] def commonMethod(self,request): tmp=self.targetHost.split('/') net=tmp[0]+'//'+tmp[2] request=request.replace(net,'') targetAddr=self.getTargetInfo(tmp[2]) try: (fam,_,_,_,addr)=socket.getaddrinfo(targetAddr[0],targetAddr[1])[0] except Exception as e: print(e) return self.target=socket.socket(fam) self.target.connect(addr) self.target.send(request.encode()) self.nonblocking() def connectMethod(self,request): #对于CONNECT处理可以添加在这里 pass def run(self): request=self.getClientRequest() self.getHeader(request) if request: if self.method in ['GET','POST','PUT',"DELETE",'HAVE']: self.commonMethod(request) elif self.method=='CONNECT': self.connectMethod(request) def nonblocking(self): inputs=[self.client,self.target] while True: readable,writeable,errs=select.select(inputs,[],inputs,3) if errs: break for soc in readable: data=soc.recv(self.BUFSIZE) if data: if soc is self.client: self.target.send(data) elif soc is self.target: self.client.send(data) else: break self.client.close() self.target.close() def getTargetInfo(self,host): port=0 site=None if ':' in host: tmp=host.split(':') site=tmp[0] port=int(tmp[1]) else: site=host port=80 return site,port if __name__=='__main__': host = '0.0.0.0' port = 8088 backlog = 5 server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) server.bind((host,port)) server.listen(5) while True: t = threading.Thread(target=Proxy(server).run) t.start() # p=Process(target=Proxy(server).run, args=()) #多进程 # p.start()
相关文章推荐
- Python入门:python实现http透明代理(二)
- Python入门:python实现http透明代理(三)
- Python实现多线程HTTP下载器示例
- python网络爬虫入门(二)——用python简单实现调用谷歌翻译
- 基于 Agent 的模型入门Python实现 读书笔记
- Python模块学习 ---- httplib HTTP协议客户端实现
- python(通过flask实现一个http接口)调用这个接口实现第三方smtp发送邮件
- Python模块学习 ---- httplib HTTP协议客户端实现
- 【转载】Python 实现的随机森林http://lidandan1314.diandian.com/
- python实现http post四种请求体x-www-form-urlencoded ,form-data ,json,xml
- python使用json web token (jwt)实现http api的加密传输
- android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例
- python实现的正则表达式功能入门教程【经典】
- Python基于pyCUDA实现GPU加速并行计算功能入门教程
- 使用python实现http及ftp服务进行数据传输的方法
- Python:httplib模块用于HTTP协议客户端实现
- 芝麻HTTP: Python爬虫入门之正则表达式
- Python模块学习 ---- httplib HTTP协议客户端实现
- python实现的入门级别死爬虫
- [原创]用python检测LVS real server状态实现HTTP高可用