aiohttp AppRunner的用法
参考廖雪峰的aiohttp教程,会出现两个DeprecationWarning,
- loop argument is deprecated
- Application.make_handler(...) is deprecated, use AppRunner API instead
解决方案
- loop 参数直接不用就是
- 参见aiohttp官网,AppRunner的用法:
The simple startup code for serving HTTP site on 'localhost', port 8080 looks like:
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()
# -*- coding: utf-8 -*-
import logging
logging.basicConfig(level=logging.INFO)
import os,json,time,asyncio
from datetime import datetime
from aiohttp import web
# def index(request): # 原始简单的url处理函数
# return web.Response(body=b'<h1>Awesome</h1>',content_type='text/html')
def init_jinja2(app, **kw): #初始化 jinja2的 env
pass
async def logger_factory(app, handler):
async def logger(request):
logging.info('Request: %s %s' % (request.method, request.path))
return (await handler(request))
return logger
# 生产 post 提交的数据
async def data_factory(app, handler):
pass
#将url处理函数的返回值 转换成 response 对象
async def response_factory(app, handler):
pass
return response
# 将blog 评论的发布时间 转换成 多少时间以前
def datetime_filter(t):
delta = int(time.time() - t)
if delta < 60:
return u'1分钟前'
if delta < 3600:
return u'%s分钟前' % (delta // 60)
if delta < 86400:
return u'%s小时前' % (delta // 3600)
if delta < 604800:
return u'%s天前' % (delta // 86400)
dt = datetime.fromtimestamp(t)
return u'%s年%s月%s日' % (dt.year, dt.month, dt.day)
async def init(loop):
db = configs.configs.db
await orm.create_pool(loop=loop, **db)
#DeprecationWarning: loop argument is deprecated
app = web.Application(loop = loop,middlewares=[ #拦截器 一个URL在被某个函数处理前,可以经过一系列的middleware的处理。
logger_factory, response_factory #工厂模式
])
init_jinja2(app, filters=dict(datetime=datetime_filter))
add_routes(app, 'handlers')
add_static(app)
# DeprecationWarning: Application.make_handler(...) is deprecated, use AppRunner API instead
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '192.168.2.101', 9000)
logging.info('server started at http://192.168.2.101:9000...')
await site.start()
#以前的写法
# srv = await loop.create_server(app.make_handler(), '192.168.2.101', 9000)
# logging.info('server started at http://192.168.2.101:9000...')
# return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()
aiohttp AppRunner的用法的更多相关文章
- 深入理解协程(四):async/await异步爬虫实战
本文目录: 同步方式爬取博客标题 async/await异步爬取博客标题 本片为深入理解协程系列文章的补充. 你将会在从本文中了解到:async/await如何运用的实际的爬虫中. 案例 从CSDN上 ...
- Python开发【模块】:aiohttp(一)
AIOHTTP 用于asyncio和Python的异步HTTP客户端/服务器 主要特点: 支持客户端和HTTP服务器. 支持服务器WebSockets和 客户端WebSockets开箱即用,没有回调地 ...
- python 调用aiohttp
1. aiohttp安装 pip3 install aiohttp 1.1. 基本请求用法 async with aiohttp.get('https://github.com') as r: a ...
- 异步:asyncio和aiohttp的一些应用(1)
1. asyncio 1.1asyncio/await 用法 async/await 是 python3.5中新加入的特性, 将异步从原来的yield 写法中解放出来,变得更加直观. 在3.5之前,如 ...
- Python使用asyncio+aiohttp异步爬取猫眼电影专业版
asyncio是从pytohn3.4开始添加到标准库中的一个强大的异步并发库,可以很好地解决python中高并发的问题,入门学习可以参考官方文档 并发访问能极大的提高爬虫的性能,但是requests访 ...
- 异步协程asyncio+aiohttp
aiohttp中文文档 1. 前言 在执行一些 IO 密集型任务的时候,程序常常会因为等待 IO 而阻塞.比如在网络爬虫中,如果我们使用 requests 库来进行请求的话,如果网站响应速度过慢,程序 ...
- 小爬爬4.协程基本用法&&多任务异步协程爬虫示例(大数据量)
1.测试学习 (2)单线程: from time import sleep import time def request(url): print('正在请求:',url) sleep() print ...
- Python调用aiohttp
1. aiohttp安装 pip install aiohttp 1.1. 基本请求用法 async with aiohttp.get('https://github.com') as r: awai ...
- python 基于aiohttp的异步爬虫实战
钢铁知识库,一个学习python爬虫.数据分析的知识库.人生苦短,快用python. 之前我们使用requests库爬取某个站点的时候,每发出一个请求,程序必须等待网站返回响应才能接着运行,而在整个爬 ...
随机推荐
- Linux c获取任意路径的硬盘使用情况
没有什么好说的,其实就是获取硬盘的statfs信息结构 代码如下: #include <stdio.h> #include <stdlib.h> #include <sy ...
- JavaScript Basic Memo
1.this 的指向 1).由 new 调用?绑定到新创建的对象. 2). 由 call 或者 apply(或者 bind)调用?绑定到指定的对象. 3). 由上下文对象调用?绑定到那个上下文对象. ...
- python之路-----python操作 mysql
========================pymysql============================ 一.pymysql 基础 安装命令:pip3 install pymysql - ...
- .net core webapi 使用ValidationAttribute对比同一对象的多个参数
众所周知,在使用DataAnnotations数据验证的时候,特别是针对同一个InputDto的多个属性进行对比的时候,例如起始日期不能大于结束日期,我们需要在Attribute中知道当前InputD ...
- 举例跟踪linux内核系统调用
学号351+ 原创作品转载请注明出处 + 中科大孟宁老师的linux操作系统分析: https://github.com/mengning/linuxkernel/ 实验要求: 编译内核5.0 qem ...
- Python正则表达式的re库一些用法(上)
1.查找文本中的模式 search()函数取模式和要扫描的文本作为输入,找到这个模式时就返回一个match对象.如果没有找到模式,search()就返回None. 每个match对象包含有关匹配性质的 ...
- TCP/IP OPTION字段
0x01 简介 TCP头部和IPV4头部除了固定的20字节外,都设置了 OPTION 字段用于存储自定义的数据,因为TCP头部和IPV4的报文长度字段均为4字节,所表示的最大值为15, 乘4,报文头部 ...
- Python语言:Day9练习题及其答案
1.使用while循环输出1,2,3,4,5,6, 8,9,10 #!/usr/bin/python3 n = 1 while n <= 10: if n != 7: print(n) else ...
- “幕后英雄”之Backing Fields【Microsoft Entity Framework Core随笔】
刘德华 有一首歌叫<马桶>,其中有一句歌词是:每一个马桶都是英雄. EFCore也有一个英雄,在幕后默默地任劳任怨.它就叫 "支持字段" (Backing Fields ...
- vue搭建
~感觉再不学点新技能,马上就要被淘汰了== 1.先安装node,直接点击下一步就OK node官网 https://nodejs.org/en/ 2.安装完成之后,打开命令行工具,快捷键是cmd ...