Python代理+线程+进程+socket
来源:互联网 发布:网络研修收获与反思 编辑:程序博客网 时间:2024/06/06 06:36
#coding=utf-8#作者:ewang#版本:2.7#功能描述:编写python版本,对特定的网站,获取相关的json值import socket import thread import urlparse import selectimport threadingimport reimport json import multiprocessingBUFLEN=8192class Proxy(object): def __init__(self,conn,addr): self.source=conn self.request=""self.Json=""self.Jrespone=""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 self.request=header[index+1:] def conn_destnation(self): ip=socket.gethostbyname("要获取数据的页面") self.destnation.connect_ex((ip,80)) data="%s %s %s\r\n" %("POST","URL","HTTP/1.1") self.destnation.send(data+self.request)item=re.findall('\"props\"\s*:(.+})\,',self.request,re.S)item=item[0].replace(" ","")#对json文件编码Jdumps=json.dumps(item,sort_keys=True)#对json文件解码Jrespone=json.loads(Jdumps)json_respone=json.loads(item)self.Json=json_responeself.Jrespone=Jresponeif self.Json["appName"]=="event":print self.Jrespone def run(self):self.get_headers()self.conn_destnation() class Server(object): def __init__(self,host,port): self.server=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.server.bind((host,port)) self.server.listen(5)self.start() def start(self):while True: try: conn,addr=self.server.accept()thread.start_new_thread(Proxy(conn,addr),())except: pass if __name__=='__main__':print u"服务器IP:",serverIp=str(raw_input())print u"服务器端口号:",serverPort=int(raw_input())print u"开始获取数据........"p = multiprocessing.Process(target = Server, args = (serverIp,serverPort))p.start()
该脚本是为了针对数据测试中,提高工作效率,针对特定于网页来进行json数据的获取,通过json数据与预期的json数据进行比对。
代码中的“URL”是特定网站的URL,可以根据需求进行改动,可以把之前的代码与此相结合,来达到自己的需求。为使代码运行快,采用线程和进程来提高运行速度
0 0
- Python代理+线程+进程+socket
- python 进程和线程
- python进程和线程
- python进程&线程
- Python进程和线程
- Python 进程与线程
- python 线程与进程
- Python 线程和进程
- python线程、进程
- Python线程进程
- Python 线程、进程、协程
- Python进程、线程、协程
- python 线程与进程
- python 进程和线程
- python中的进程, 线程
- python中线程进程
- Python进程和线程
- python进程、线程、协程
- WIFI的Log切入点
- 2017届毕业生安卓面试题-JAVA篇
- *****************************自己总结的小技巧**********************************
- 24 AIDL案例
- hd 1065 I Think I Need a Houseboat(数学、水)
- Python代理+线程+进程+socket
- HDU 5381The sum of gcd 莫队算法
- 可变参数【Java】
- LA2995- Image Is Everything
- MDK在链接时提示空间不够(No space in execution regions with .ANY selector... )的解决方案总结
- lua封装的位运算
- ORA-00313,ORA-19527
- java基于GUI的定时关机程序
- Retrofit2.0的离线缓存