java中的接口是类吗
258
2022-08-28
python的http代理,对请求的头文件中的json数据进行比对-------(2)
#coding=utf-8#作者:ewang#版本:2.7#功能描述:编写python版本,对特定的网站,获取相关的json值import 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.Jrespone="" 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文件编码 Jdumps=json.dumps(item,sort_keys=True) #对json文件解码 Jrespone=json.loads(Jdumps) json_respone=json.loads(item) if contentType and xmlyHost: self.Json=json_respone self.Jrespone=Jrespone #print self.request #print item #print json_dumps.keys() #print self.Json.keys() def Compare_Json(self): Json_File="C:\\Users\\ewang\\Desktop\\realJson.json" JHandler=open(Json_File,"rb") try: JsReader=JHandler.readlines() for line in JsReader: ldumps=json.dumps(line,sort_keys=True) lloads=json.loads(ldumps) # for real_key in self.Json.keys(): # for exp_key in line_loads.keys(): # real_key=real_key.decode("utf-8") # exp_key=exp_key.decode("utf-8") # real_value=self.Json[real_key] # exp_value=line_loads[exp_key] # if real_key==exp_key and real_value==exp_value: # print u"实际结果与预期结果一致!\n" # break #Expect_Json={} #Expect_Json=line_loads #if Real_Json==Expect_Json: #print "SUCCESS" #else: #print "Failur\n" #continue line_loads=json.loads(line) #筛选appName=u'event',然后比较json值 for real_key in self.Json.keys(): #real_key=real_key.decode("utf-8") if self.Json[real_key]=="event": if cmp(self.Json,line_loads)==0: print u"实际结果与预期结果一致!\n" print "*"*40 print lloads print self.Jrespone print "*"*40 else: pass # print "*"*40 # print lloads # print self.Jrespone # print "*"*40 finally: JHandler.close() 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() self.Compare_Json() 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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~