java 单机接口限流处理方案
239
2022-08-29
Python的HTTP非阻塞代理V1.0
__init__.py
from proxy import Proxyfrom server import Serverimport judge
judge.py
#coding=utf8 def methodJudge(method): if method == 'POST': return True else: return False def protoclJudge(protocol): #请求的链接URL if protocol==' return True else: return False def serverJudge(server): #判断服务是否为:xdcs-collector if server=="xdcs-collector": return True else: return False def interfaceJudge(interface): if interface=='/api/v1/statistics': return True else: return False
proxy.py
#coding=utf8import socket,selectfrom judge import serverJudgeclass Proxy(object): 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) 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] if serverJudge(self.targetHost): return request 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) self.nonblocking() def connectMethod(self,request): pass def run(self): request=self.getClientRequest() if request: #if self.method in ['GET','POST','PUT',"DELETE",'HAVE']: if self.method in ['POST']: self.commonMethod(request) elif self.method=='CONNECT': #self.connectMethod(request) pass def nonblocking(self): inputs=[self.client,self.target] while True: #readable,writeable,errs=select.select(inputs,[],inputs,3) readable,_,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
server.py
#coding=utf8import socketfrom proxy import Proxyimport threaddef Server(host,port): #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: thread.start_new_thread(Proxy(server).run,()) def test(): print "run test()" host="192.168.62.33" port=8888 Server(host, port)if __name__=='__main__': test()
Run.py
#coding=utf8from Lib import Server'''创建一个函数用来运'''def run(): host=raw_input("Enter the hostIP:") port=raw_input("Enter the port:") print "Server HTTP On %s Port %s" %(host,port) Server(host,int(port)) if __name__=='__main__': run()
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~