• socket发送请求,协程


    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
    host:www.baidu.com
    
    ")
    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(b'GET /s?wd=alex HTTP/1.0
    host:www.baidu.com
    
    ')
    36         elif sk == client2:
    37             sk.sendall(b'GET /web?query=fdf HTTP/1.0
    host:www.sogou.com
    
    ')
    38         else:
    39             sk.sendall(b'GET /s?wd=alex HTTP/1.0
    host:www.oldboyedu.com
    
    ')
    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 )
  • 相关阅读:
    Android开发探秘之一:创建可以点击的Button
    TCP之心跳包实现思路
    Asp.net与Dojo交互:仪器仪表实现
    Asp.net Json数据解析的一种思路
    Asp.net通过Jquery操作WebService进行Ajax读写
    GridView自定义删除操作
    从客户端中检测到有潜在危险的request.form值
    JUC-Condition线程通信
    ModelAndView 配置与使用
    SpringMVC之ModelAndView的用法(转)
  • 原文地址:https://www.cnblogs.com/s593941/p/9643246.html
Copyright © 2020-2023  润新知