python编写http代理获取json数据----------------------(1)

网友投稿 270 2022-08-28


python编写http代理获取json数据----------------------(1)

#coding=utf-8import socket import thread import urlparse import selectimport threadingimport reimport json BUFLEN=8192 class Proxy(object): def __init__(self,conn,addr): self.source=conn self.request="" self.Json="" self.headers={} self.destnation=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.run() def get_headers(self): header='' while True: header+=self.source.recv(BUFLEN) index=header.find('\n') if index >0: break #firstLine,self.request=header.split('\r\n',1) firstLine=header[:index] self.request=header[index+1:] self.headers['method'],self.headers['path'],self.headers['protocol']=firstLine.split() def conn_destnation(self): url=urlparse.urlparse(self.headers['path']) hostname=url[1] port="80" if hostname.find(':') >0: addr,port=hostname.split(':') else: addr=hostname port=int(port) ip=socket.gethostbyname(addr) #print ip,port self.destnation.connect((ip,port)) data="%s %s %s\r\n" %(self.headers['method'],self.headers['path'],self.headers['protocol']) self.destnation.send(data+self.request) contentType=re.search(r'{',self.request,re.S) xmlyHost=re.search(r'Host: (.*)\.ximalaya\.(.*)',self.request,re.S) item=re.findall('({.+)',self.request,re.S) #filename=re.findall('"serviceId" : "(\w+)"',self.request,re.S) item=item[0].replace("\n ","").decode("utf-8") item=re.findall('\"props\"\s*:(.+})\,',item,re.S) item=item[0].replace(" ","") #对json文件编码 json_dumps=json.dumps(item,sort_keys=True) #对json文件解码 json_respone=json.loads(json_dumps) if contentType and xmlyHost: print self.request #print item #print json_dumps print json_respone def renderto(self): readsocket=[self.destnation] while True: data='' (rlist,wlist,elist)=select.select(readsocket,[],[],3) if rlist: data=rlist[0].recv(BUFLEN) if len(data)>0: self.source.send(data) else: break def run(self): self.get_headers() self.conn_destnation() self.renderto() class Server(object): def __init__(self,host,port): self.host=host self.port=port self.server=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server.bind((host,port)) self.server.listen(5) def start(self): while True: try: conn,addr=self.server.accept() Proxy(conn,addr) except: pass s=Server('127.0.0.1',8889) threads=[] for i in range(1): t=threading.Thread(target=s.start,args=()) threads.append(t)if __name__=='__main__': #启动线程 for i in threads: i.start() #keep thread for i in threads: i.join()


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python脚本删除指定路径下指定类型的文件(python怎样删除文件中的指定内容)
下一篇:关于springboot加载yml配置文件的no字段自动转义问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~