Python Loop一遍又一遍地重复该过程

时间:2019-01-15 09:29:31

标签: python python-3.x

我是Web开发的新手,我正在通过发送http get请求来测试我的网站,以检查我的网站将如何处理请求。使用我的代码,我可以发送多个get请求,如何使代码发送多个请求,我希望循环永不停止,我的意思是一次又一次发送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))

1 个答案:

答案 0 :(得分: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))