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库爬取某个站点的时候,每发出一个请求,程序必须等待网站返回响应才能接着运行,而在整个爬 ...
随机推荐
- IntelliJ IDEA2018.3 最新破解方法
IntelliJ IDEA2018.3 最新破解方法 输入 http://idea.java.sx/ 即可,亲测可用.如果资金允许还是希望大家能支持正版,尊重原创 ------------- ...
- GET 和 POST 请求的优缺点和误区
Get和Post在面试中一般都会问到,一般的区别:(1)post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及浏览器浏览记录中)(2)post发送的数据更大(get有url长度限制 ...
- YML文件中ipv6地址输入格式
关于YML文件格式可以百度,这里只说ipv6:yml文件是注重格式的不能用tab键代替空格 ipv4 :10.1.202.9 ipv6: 2001:202::6e4:f32b:c19c:4760 端口 ...
- js实现原生选项卡
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...
- SoapUI之cookie设置
一.测试背景: 1)接口测试需要完成注册-->登录-->充值,使用soapui构建好测试用例.设置断言后,运行结果如下: 2)recharge接口运行失败,继续查看该接口具体发送的请求及返 ...
- nginx常用模块
Nginx模块介绍 核心模块:core module 标准模块:stand modules HTTP modules: Standard HTTP modules Optional HTTP modu ...
- tensorflow 只恢复部分模型参数
import tensorflow as tf def model_1(): with tf.variable_scope("var_a"): a = tf.Variable(in ...
- 第四周博客之一---Linux的基本命令(前5个)
一.Linux的系统结构 "/"根目录部分有以下子目录: 1./bin:系统启动时需要的执行文件(二进制),这些文件可以被普通用户使用. 2./boot:用于自举加载程序(LILO ...
- mybatis源码解析之Configuration加载(一)
概要 上一篇,我们主要搭建了一个简单的环境,这边我们主要来分析下mybatis是如何来加载它的配置文件Configuration.xml的. 分析 public class App { public ...
- Python随笔--魔法方法(析构与构造)
#析构方法的调用