aiohttp

asyncio/aiohttp - create_task() blocks event loop, gather results in “This event loop is already running ”

假如想象 提交于 2021-01-01 08:12:18
问题 I cannot get both my consumer and my producer running at the same time, it seems worker(), or the aiohttp server are blocking - even when executed simultaneously with asyncio.gather() If instead I do loop.create_task(worker), this will block and server will never be started. I've tried every variation I can imagine, including nest_asyncio module - and I can only ever get one of the two components running. What am I doing wrong? async def worker(): batch_size = 30 print("running worker") while

Peer to peer client with peerjs and python user

老子叫甜甜 提交于 2020-12-15 05:44:14
问题 Download and unrar this file in htdocs folder (for example C:/xampp/htdocs/telephone_calls) Run this ngrok command: ngrok http 192.168.1.24:80 (replace 192.168.1.24 with your lan ip address) Replace lines 118 and 119 of file manage_calls.py with your own. cd to telephone_calls directory and run python manage_calls.py With another device open a browser and go to https://25f4a7e689ce.ngrok.io/telephone_calls/client.html (replace 25f4a7e689ce with your own) Press the blue button "Τηλεφωνική

软件测试开发实战|接口自动化测试框架开发(pytest+allure+aiohttp+用例自动生成)

寵の児 提交于 2020-12-11 08:41:36
近期准备优先做接口测试的覆盖,为此需要开发一个测试框架,经过思考,这次依然想做点儿不一样的东西。 接口测试是比较讲究效率的,测试人员会希望很快能得到结果反馈,然而接口的数量一般都很多,而且会越来越多,所以提高执行效率很有必要 接口测试的用例其实也可以用来兼做简单的压力测试,而压力测试需要并发 接口测试的用例有很多重复的东西,测试人员应该只需要关注接口测试的设计,这些重复劳动最好自动化来做 pytest和allure太好用了,新框架要集成它们 接口测试的用例应该尽量简洁,最好用yaml,这样数据能直接映射为请求数据,写起用例来跟做填空题一样,便于向没有自动化经验的成员推广 加上我对Python的协程很感兴趣,也学了一段时间,一直希望学以致用,所以http请求我决定用aiohttp来实现。 但是pytest是不支持事件循环的,如果想把它们结合还需要一番功夫。于是继续思考,思考的结果是其实我可以把整个事情分为两部分。 第一部分,读取yaml测试用例,http请求测试接口,收集测试数据。 第二部分,根据测试数据,动态生成pytest认可的测试用例,然后执行,生成测试报告。 这样一来,两者就能完美结合了,也完美符合我所做的设想。想法既定,接着 就是实现了。 第一部分(整个过程都要求是异步非阻塞的) 读取yaml测试用例 一份简单的用例模板我是这样设计的,这样的好处是,参数名和aiohttp

新一代的网络请求库 Httpx

空扰寡人 提交于 2020-12-08 03:32:19
点击上方“ Python学习开发 ”,选择“ 加为星标 ” 第一时间关注Python技术干货! 简介 HTTPX 是最近 GitHub看的到一个比较火的一个项目,根据官网的描述,总结有如下特点: 和使用 requests 一样方便,requests 有的它都有 加入 HTTP/1.1 和 HTTP/2 的支持。 能够直接向 WSGI 应用程序或 ASGI 应用程序发出请求。 到处都有严格的超时设置 全类型注释 100% 的测试覆盖率 比较不错的一个特点是全类型注解,这让我想起了一个叫 Starlette 的库,它也是全类型注解的,类型注解主要方便IDE的智能提示,Java 等静态类型的语言都有这个功能,Python 是近期新加的。其他的后面再说吧,我们还是看例子吧。 安装 httpx 的安装很简单,像其他的 Python 库一样,直接 pip 就完事了 python3 -m pip install httpx 如果需要对 HTTP/2 支持,我们需要额外安装一个库 python3 -m pip install httpx [http2] 使用示例 import httpx r = httpx. get ( 'https://www.example.org/' ) r.text r.content r.json() r.status_code 基本的用法直接导包然后 get 就行了

为 aiohttp 爬虫注入灵魂

久未见 提交于 2020-12-04 13:23:52
为 aiohttp 爬虫注入灵魂 摄影:产品经理 与产品经理在苏州的小生活 听说过异步爬虫的同学,应该或多或少听说过aiohttp这个库。它通过 Python 自带的async/await实现了异步爬虫。 使用 aiohttp,我们可以通过 requests 的api写出并发量匹敌 Scrapy 的爬虫。 我们在 aiohttp 的官方文档上面,可以看到它给出了一个代码示例,如下图所示: 我们现在稍稍修改一下,来看看这样写爬虫,运行效率如何。 修改以后的代码如下: import asyncio import aiohttp template = 'http://exercise.kingname.info/exercise_middleware_ip/{page}' async def get(session, page): url = template.format(page=page) resp = await session.get(url) print(await resp.text(encoding='utf-8')) async def main(): async with aiohttp.ClientSession() as session: for page in range(100): await get(session, page) loop = asyncio

“RuntimeError: This event loop is already running”; debugging aiohttp, asyncio and IDE “spyder3” in python 3.6.5

回眸只為那壹抹淺笑 提交于 2020-12-01 10:18:33
问题 I'm struggling to understand why I am getting the "RuntimeError: This event loop is already running" runtime error. I have tried to run snippets of code from "https://aiohttp.readthedocs.io/en/stable/" however, I keep getting the same issue. Code snippet from Tutorial: import aiohttp import asyncio import async_timeout async def fetch(session, url): async with async_timeout.timeout(10): async with session.get(url) as response: return await response.text() async def main(): async with aiohttp