#coding=utf-8
#作者:ewang
#版本:2.7
#功能描述:编写python版本,对特定的网站,获取相关的json值
import socket
import thread
import urlparse
import select
import threading
import re
import 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=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)
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)
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

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)
line_loads=json.loads(line)
key_count=0
for exp_key in line_loads.keys():
for real_key in self.Json.keys():
if exp_key==real_key and ((self.Json[real_key]==line_loads[exp_key]) or (line_loads[exp_key]=="" and self.Json[real_key]!=None)):
key_count+=1
else:
pass
if key_count==len(line_loads.keys()):
print "pass"
print u"预期结果:",lloads
print u"实际结果:",self.Jrespone
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


threads=[]
for i in range(5):
s=Server('127.0.0.1',8889)
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()