zoukankan      html  css  js  c++  java
  • 异步网络编程aiohttp的使用

    aiohttp的使用

    aiohttp

    Asynchronous HTTP Client/Server for asyncio and Python.
    Supports both Client and HTTP Server.
    Supports both Server WebSockets and Client WebSockets out-of-the-box without the Callback Hell.
    Web-server has Middlewares, Signals and pluggable routing.

    安装:

    pip install cchardet
    pip install aiohttp
    pip install asyncio
    
    

    因为有的网站请求的时候会验证ssl证书,如果是自签名的ssl证书会出错。

    conn=aiohttp.TCPConnector(verify_ssl=False)#防止ssl报错
    

    class aiohttp.TCPConnector(*, ssl=None, verify_ssl=True, fingerprint=None, use_dns_cache=True, ttl_dns_cache=10, family=0, ssl_context=None, local_addr=None, resolver=None, keepalive_timeout=sentinel, force_close=False, limit=100, limit_per_host=0, enable_cleanup_closed=False, loop=None)

    limit

    为了限制同时打开的连接数量,我们可以将限制参数传递给连接器:
    
    conn = aiohttp.TCPConnector(limit=30)#同时最大进行连接的连接数为30,默认是100,limit=0的时候是无限制
    limit_per_host:
    
    conn = aiohttp.TCPConnector(limit_per_host=30)#默认是0
    同一端点的最大连接数量。同一端点即(host, port, is_ssl)完全相同.
    

    ClientSession

    首先我们创建一个session对象,向下面这样使用async声明异步,同时with上下文关键字 省去了关闭连接的代码,

    async with aiohttp.ClientSession(connector=conn) as session: 
    

    下面是ClientSession的所有参数,这里用的比较多的是connector,headers,cookies其他的参数大家可以去自己探索一下。

    class aiohttp.ClientSession(*, connector=None, loop=None, cookies=None, headers=None, skip_auto_headers=None, auth=None, json_serialize=json.dumps, version=aiohttp.HttpVersion11, cookie_jar=None, read_timeout=None, conn_timeout=None, timeout=sentinel, raise_for_status=False, connector_owner=True, auto_decompress=True, proxies=None

    session.get

    上面我们创建了session对象,然后我们就要进行请求具体的网站了。

    async with session.get(url,headers=headers,timeout=60) as req: #获得请求
    

    这一步我们像使用requests那样传入headers参数并指定最大超时为60s。

    ClientResponse

    然后我们判断请求是否情况,之后我们使用await req.text()获取了网页的源码,注意这里必须使用await关键字来获取协程的结果。然后我们使用了lxml模块获取这三个网页的title标题。

    到目前为止我们只是定义了一个协程,并没有真正的运行它接下来我们看看main方法是做什么的。

    full_urllist= ["https://www.baidu.com","https://www.cnblogs.com","https://www.jianshu.com"]         event_loop = asyncio.get_event_loop() #创建时间循环         
    tasks = [getsource(url) for url in full_urllist]         
    results = event_loop.run_until_complete(asyncio.wait(tasks))#等待任务结束
    

    首先我定义了一个列表含有三个目标url,当前你可以定义更多。
    asyncio.get_event_loop方法可以创建一个事件循环,然后使用run_until_complete将协程注册到事件循环,并启动事件循环。
    协程对象不能直接运行,在注册事件循环的时候,其实是run_until_complete方法将协程包装成为了一个任务(task)对象。所谓task对象是Future类的子类。保存了协程运行后的状态,用于未来获取协程的结果。

    asyncio.ensure_future(coroutine) 和 loop.create_task(coroutine)都可以创建一个task,run_until_complete的参数是一个futrue对象。当传入一个协程,其内部会自动封装成task,task是Future的子类。isinstance(task, asyncio.Future)将会输出True

  • 相关阅读:
    win7 php 配置多个网站
    win7 ShuipFCMS 配置 及问题
    【转】CentOS 6 服务器安全配置指南
    好的博客 网址
    【转】管理员必备的Linux系统监控工具
    【转】centos安装memcached+php多服务器共享+session多机共享问题
    [转]CentO下限制SSH登录次数
    使用Atlas实现MySQL读写分离+MySQL-(Master-Slave)配置
    centos 内网ip访问mysql数据库
    [转]Centos6.5安装配置keepalived
  • 原文地址:https://www.cnblogs.com/ruhai/p/11134713.html
Copyright © 2011-2022 走看看