zoukankan      html  css  js  c++  java
  • 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 )
  • 相关阅读:
    IDEA(jetbrain通用)优雅级使用教程(转)
    intellij idea 修改背景保护色&&修改字体&&快捷键大全(转)
    淘宝可伸缩高性能互联网架构HSF(转)
    spring利用注解@Value获取properties属性为null
    String.valueOf()方法注意
    Spring任务调度器之Task的使用(转)
    Mybatis集成(转)
    深入理解mybatis原理, Mybatis初始化SqlSessionFactory机制详解(转)
    Spring+Mybatis+SpringMVC+Maven+MySql搭建实例(转)
    Spring4.2+SpringMVC+Mybatis3.4的集成(转-)
  • 原文地址:https://www.cnblogs.com/s593941/p/9643246.html
Copyright © 2011-2022 走看看