python - Python Loop 一遍遍地重复这个过程
问题描述
我是 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))
解决方案
如果您希望某事一遍又一遍地发生,请添加一个for或while循环 - 请参阅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))
推荐阅读
- ruby-on-rails - 使用 Active Storage / S3、React 和 ActiveAdmin 上传 Rails API 图像
- python - 绘制特征重要性会给出错误“数组必须都是相同的长度”
- python - 如何从 BeautifulSoup 中提取字典/列表
- extjs - Sencha Ext JS 6 - 如何替换路由控制器
- hibernate - 如何告诉 Hibernate 使用代理?
- python - 为什么 print('\a') 在我的 python 代码中不起作用?
- javascript - 表单验证执行不正确
- arrays - 封闭范围数组
和反向收集 > - iis - 使用 ColdFusion 重写 IIS URL 会保留重复的字符串
- jquery - jQuery 更改只工作两次