相关疑难解决方法(0)

异步aiohttp请求失败,但同步请求成功

使用以下代码,我Cannot connect to host ...:443 ssl:True在使用异步时获得aiohttp.当我使用同步时requests,它会成功.

whitehouse.gov链接失败,但google.com成功的两个异步和同步情况.

出了什么问题?这是在FreeBSD8上的python 3.4.2,aiohttp 0.14.4,请求2.5.3

import asyncio
import aiohttp
import requests

urls = [
    'http://www.whitehouse.gov/cea/', 
    'http://www.whitehouse.gov/omb', 
    'http://www.google.com']


def test_sync():
    for url in urls:
        r = requests.get(url)
        print(r.status_code)


def test_async():
    for url in urls:
        try:
            r = yield from aiohttp.request('get', url)
        except aiohttp.errors.ClientOSError as e:
            print('bad eternal link %s: %s' % (url, e))
        else:
            print(r.status)


if __name__ == '__main__':
    print('async')
    asyncio.get_event_loop().run_until_complete(test_async())
    print('sync')
    test_sync()
Run Code Online (Sandbox Code Playgroud)

这个输出是:

async
bad …
Run Code Online (Sandbox Code Playgroud)

asynchronous python-3.x python-asyncio aiohttp

6
推荐指数
2
解决办法
6052
查看次数

使用aiohttp / asyncio的异步HTTP调用失败,并显示“无法连接到主机[网络不可达]”

我正在实现一个快速异步REST接口调用程序,我想通过该调用程序以同步方式上载数据。现在,我将要构建一个异步框架,该框架仅以异步方式调用python页面并测量延迟。

这是代码(如下所述不起作用):

import aiohttp
import asyncio
import async_timeout
from timeit import default_timer as timer

async def fetch(session, url):
    start = timer()
    with async_timeout.timeout(10):
        async with session.get(url) as response:
            date = response.headers.get("DATE")
            end = timer()
            delay = end - start
            print("{}:{} with delay {} s".format(date, response.url, delay))
            return await response.read()

async def bound_call(semaphore, session, url):
    async with semaphore:
        await fetch(session, url)

async def perform_call(session):
    sem = asyncio.Semaphore(1000)
    html = await bound_call(sem, session, 'http://python.org')

async def perform_calls(n):
    tasks = []
    async with …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio aiohttp

5
推荐指数
1
解决办法
1506
查看次数