1. 程式人生 > >socket發送請求,協程

socket發送請求,協程

from set strong lec mon move .org col str

1.socket發送請求

 1 #發送請求的方式
 2 
 3 #方式一
 4 import requests
 5 
 6 ret = requests.get("https://www.baidu.com/s?wd=abc")
 7 
 8 print(ret.text)
 9 
10 
11 #方式二
12 
13 import socket
14 
15 client = socket.socket()
16 client.connect(("www.baidu.com",80))
17 client.sendall(b"GET /s?wd=alex HTTP/1.0\r\nhost:www.baidu.com\r\n\r\n
") 18 chunk_list = [] 19 while True: 20 chunk = client.recv(8096) 21 if not chunk: 22 break 23 chunk_list.append(chunk) 24 25 body = b"".join(chunk_list) 26 print(body.decode("utf-8"))

  單線程的並發

 1 import socket
 2 import select
 3 
 4 client1 = socket.socket()
 5 client1.setblocking(False)#
百度創建鏈接:非阻塞 6 7 try: 8 client1.connect(("www.baidu.com",80)) 9 except BlockingIOError as e: 10 pass 11 12 client2 = socket.socket() 13 client2.setblocking(False) 14 15 try: 16 client2.connect(("www.baidu.com",80)) 17 except BlockingIOError as e: 18 pass 19 20 client3 = socket.socket()
21 client3.setblocking(False) 22 23 try: 24 client3.connect(("www.baidu.com",80)) 25 except BlockingIOError as e: 26 pass 27 28 socket_list = [client1,client2,client3] 29 conn_list = [client1,client2,client3] 30 31 while True: 32 rlist,wlist,elist = select.select(socket_list,conn_list,[],0.005) 33 for sk in wlist: 34 if sk == client1: 35 sk.sendall(bGET /s?wd=alex HTTP/1.0\r\nhost:www.baidu.com\r\n\r\n) 36 elif sk == client2: 37 sk.sendall(bGET /web?query=fdf HTTP/1.0\r\nhost:www.sogou.com\r\n\r\n) 38 else: 39 sk.sendall(bGET /s?wd=alex HTTP/1.0\r\nhost:www.oldboyedu.com\r\n\r\n) 40 conn_list.remove(sk) 41 for sk in rlist: 42 chunk_list = [] 43 while True: 44 try: 45 chunk = sk.recv(8096) 46 if not chunk: 47 break 48 chunk_list.append(chunk) 49 except BlockingIOError as e: 50 break 51 body = b"".join(chunk_list) 52 53 sk.close() 54 socket_list.remove(sk) 55 if not socket_list: 56 break

2.協程

 1 import greenlet
 2 
 3 def f1():
 4     print(111)
 5     g2.switch()
 6     print(222)
 7     g2.switch()
 8 
 9 def f2():
10     print(333)
11     g1.switch()
12     print(444)
13 
14 g1 = greenlet.greenlet(f1)
15 g2 = greenlet.greenlet(f2)
16 g1.switch()

協程的IO切換

 1 from gevent import monkey
 2 monkey.patch_all()
 3 import requests
 4 import gevent
 5 
 6 def get_page1(url):
 7     ret = requests.get(url)
 8     print(url,ret.content)
 9 
10 def get_page2(url):
11     ret = requests.get(url)
12     print(url,ret.content)
13 
14 def get_page3(url):
15     ret = requests.get(url)
16     print(url,ret.content)
17 
18 gevent.joinall(
19       gevent.spawn(get_page1,https://www.python.org/),
20       gevent.spawn(get_page2, https://www.yahoo.com/),
21       gevent.spawn(get_page3, https://github.com/)   
22 
23 )

socket發送請求,協程