0

我是 Web 开发的新手,我正在通过发送 http get 请求来测试我的网站,以检查我的网站处理请求的能力。用我的代码我可以发送多个获取请求,我怎样才能让代码发送多个请求我希望循环永远不会停止,我的意思是一遍又一遍地发送获取请求我该怎么做..我很抱歉我的英语不好希望你能得到我的问题。

import time
import datetime
import asyncio
import aiohttp

domain = 'http://myserver.com'
a = '{}/page1?run={}'.format(domain, time.time())
b = '{}/page2?run={}'.format(domain, time.time())

async def get(url):
    print('GET: ', url)
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            t = '{0:%H:%M:%S}'.format(datetime.datetime.now())
            print('Done: {}, {} ({})'.format(t, response.url, response.status))

loop = asyncio.get_event_loop()
tasks = [
    asyncio.ensure_future(get(a)),
    asyncio.ensure_future(get(b))
]
loop.run_until_complete(asyncio.wait(tasks))
4

1 回答 1

0

如果您希望某事一遍又一遍地发生,请添加一个forwhile循环 - 请参阅https://docs.python.org/3/tutorial/index.html

async def get(url):
    async with aiohttp.ClientSession() as session:
        while True:
            print('GET: ', url)
            async with session.get(url) as response:
                t = '{0:%H:%M:%S}'.format(datetime.datetime.now())
                print('Done: {}, {} ({})'.format(t, response.url, response.status))
于 2019-01-15T09:44:55.567 回答