1.socket发送请求
#发送请求的方式 #方式一
import requests ret = requests.get("https://www.baidu.com/s?wd=abc") print(ret.text) #方式二 import socket client = socket.socket()
client.connect(("www.baidu.com",80))
client.sendall(b"GET /s?wd=alex HTTP/1.0\r\nhost:www.baidu.com\r\n\r\n")
chunk_list = []
while True:
chunk = client.recv(8096)
if not chunk:
break
chunk_list.append(chunk) body = b"".join(chunk_list)
print(body.decode("utf-8"))
单线程的并发
import socket
import select client1 = socket.socket()
client1.setblocking(False)#百度创建链接:非阻塞 try:
client1.connect(("www.baidu.com",80))
except BlockingIOError as e:
pass client2 = socket.socket()
client2.setblocking(False) try:
client2.connect(("www.baidu.com",80))
except BlockingIOError as e:
pass client3 = socket.socket()
client3.setblocking(False) try:
client3.connect(("www.baidu.com",80))
except BlockingIOError as e:
pass socket_list = [client1,client2,client3]
conn_list = [client1,client2,client3] while True:
rlist,wlist,elist = select.select(socket_list,conn_list,[],0.005)
for sk in wlist:
if sk == client1:
sk.sendall(b'GET /s?wd=alex HTTP/1.0\r\nhost:www.baidu.com\r\n\r\n')
elif sk == client2:
sk.sendall(b'GET /web?query=fdf HTTP/1.0\r\nhost:www.sogou.com\r\n\r\n')
else:
sk.sendall(b'GET /s?wd=alex HTTP/1.0\r\nhost:www.oldboyedu.com\r\n\r\n')
conn_list.remove(sk)
for sk in rlist:
chunk_list = []
while True:
try:
chunk = sk.recv(8096)
if not chunk:
break
chunk_list.append(chunk)
except BlockingIOError as e:
break
body = b"".join(chunk_list) sk.close()
socket_list.remove(sk)
if not socket_list:
break
2.协程
import greenlet def f1():
print(111)
g2.switch()
print(222)
g2.switch() def f2():
print(333)
g1.switch()
print(444) g1 = greenlet.greenlet(f1)
g2 = greenlet.greenlet(f2)
g1.switch()
协程的IO切换
from gevent import monkey
monkey.patch_all()
import requests
import gevent def get_page1(url):
ret = requests.get(url)
print(url,ret.content) def get_page2(url):
ret = requests.get(url)
print(url,ret.content) def get_page3(url):
ret = requests.get(url)
print(url,ret.content) gevent.joinall(
gevent.spawn(get_page1,'https://www.python.org/'),
gevent.spawn(get_page2, 'https://www.yahoo.com/'),
gevent.spawn(get_page3, 'https://github.com/') )