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库爬取某个站点的时候,每发出一个请求,程序必须等待网站返回响应才能接着运行,而在整个爬 ...
随机推荐
- 洛谷P3627[APOI2009] (讨厌的)抢掠计划
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
- 第7天:Q Quant库(未完待续)
一.本文大纲: 1.Python内置函数计算期权的价格 2.numpy加速数值计算 3.SciPy进行仿真模拟 4.SciPy求解器计算隐含波动率 5.matplotlib绘图 二.案例 (看不懂,略 ...
- python之路-----django 自定义cookie签名
1.默认自定义cookie 在使用扩展签名时,会根据settings 配置中的 SIGNING_BACKEND 来运行加密方法,默认使用 django.core.signing.TimestampS ...
- JS-5-循环
循环结构 循环:反复执行有关操作 * for循环 for(var i=1; i<=50; i++) { console.log("第"+i+"次说我爱你" ...
- Centos 配置mailx使用外部smtp发送邮件
安装mailx yum install mailx 配置mailx 笔者推荐163邮箱,当然,QQ邮箱也是可以的,PS:记得要进邮箱打开SMTP vi /etc/mail.rc //如果不存在,则编辑 ...
- Python输入数组(一维数组、二维数组)
一维数组: arr = input("") //输入一个一维数组,每个数之间使空格隔开 num = [int(n) for n in arr.split()] //将输入每个数以空 ...
- ios调用Google地图
现在的ios版本一般只支持https协议,而引用谷歌地图API时只提供src="http://maps.google.cn/maps/api/js..",https协议无法使用,解 ...
- jQuery的 ready() 和原生 Js onload() 的主要区别:
1. onload() 会等到DOM 元素和图片都加载完毕后才执行:raeady()会等到DOM元素加载完毕,但不会等到图片加载完毕就会执行. 2.onload() 如果编写了多个,则后面的会覆盖掉前 ...
- Python 习题一
1.使用while循环输入 1 2 3 4 5 6 8 9 10 # Author:Tony.lou i = 1 while i < 11: if i == 7: pass else: prin ...
- 从零搭建Vue开发环境
参考: 1.node安装和环境配置:https://www.cnblogs.com/zhouyu2017/p/6485265.html: 2.babel的安装:https://www.cnblogs. ...