基于IO多路复用实现WEB框架
基于IO多路复用实现的webserver
import socket import select class MyWeb(object): # def __init__(self,routers): # self.routers=routers def sock_data(self,conn): # data=bytes() # while True: # try: # test=conn.recv(1024) # except Exception as e: # test=None # if not test: # break # data = data + test data=conn.recv(1024) data_str=str(data,encoding="utf-8") a = data_str.split("\r\n") data_dict = {} data_dict["method"], data_dict["lujing"], data_dict["version"] = a[0].split() a.remove(a[0]) # print(a) for i in a: cc = i.split(':') if len(cc) == 2: data_dict[cc[0]] = cc[1] return data_dict def run(self,host="127.0.0.1",port=8888): sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.bind((host,port)) sock.listen(5) inputs=[sock,] while True: r,w,e=select.select(inputs,[],[],0.5) for client in r: if client==sock: conn,addr=client.accept() inputs.append(conn) else: data_dict=self.sock_data(client) print(data_dict) client.send(b"HTTP/1.1 200 OK\r\n\r\n") client.send(b"Hello,world") inputs.remove(client) client.close() test=MyWeb() test.run()
基于IO多路复用socket客户端
爬虫(一个线程,同时发n个请求,异步非阻塞模块)
url_list=[]
a,循环列表,为每一个URL生成socket对象
b,每一个socket对象,向远程发送连接请求
c,如果连接,发送数据,遵循格式
d,获取响应内容
e,关闭
import socket import select def f1(data): print(data) url_list=[ {"host":"www.baidu.com","url":"/","callback":f1}, {"host":"www.cnblogs.com","url":"/","callback":f1}, {"host": "www.oldboyedu.com", "url": "/", "callback": f1}, ] class Foo(object): def __init__(self,sock,callback,url,host): self.sock=sock self.callback=callback self.url=url self.host=host def fileno(self): return self.sock.fileno() class YbIo(object): def __init__(self): self.rList=[] self.wList=[] def connect(self,url_list): for item in url_list: conn=socket.socket() conn.setblocking(False) try: conn.connect((item["host"],80)) except BlockingIOError as e: pass obj=Foo(conn,item["callback"],item["url"],item["host"]) self.rList.append(obj) self.wList.append(obj) def send(self): while True: try: if len(self.rList)==0: return r,w,e=select.select(self.rList,self.wList,self.rList,0.5) for obj in r: conn=obj.sock data=bytes() while True: try: d=conn.recv(1024) data = data+ d except BlockingIOError as e: d=None if not d : break obj.callback(data) self.rList.remove(obj) for obj in w: # 2.已经连接上远程 conn = obj.sock # 3. 发送数据 # HTTP/1.\r\nHost: %s\r\n\r\n template = "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n" %(obj.url,obj.host,) # template = "POST %s HTTP/1.1\r\nHost: 127.0.0.1:8888\r\n\r\nk1=v1&k2=v2" %(obj.url,) conn.sendall(template.encode('utf-8')) self.wList.remove(obj) except OSError as e: pass obj=YbIo() obj.connect(url_list) obj.send()