如何在 python 中同时打开 2 个 URL
How to open 2 URLs at the same time in python
我正在想办法同时打开两个 URL 以节省时间。
正如您在这里看到的那样,如果 10.10.10.2 可以访问,则程序将关闭,如果 192.168.100.5 可以访问,则程序将继续。
对于这个业余问题,我很抱歉,我对编程很陌生,所以请放轻松。
我下面的解决方案需要很多时间,因为它一个接一个地发生。
我正在 python 中编写一个小型检查工具来查看设备是否具有正确的 IP 地址。所以 10.10.10.2 的设备有故障,这是正确的 IP 地址 192.168.100.5。
有超过 8000 台设备需要测试,因此为每个设备等待 10 秒会花费太多时间。
你们知道同时调用 2 个不同 URL 的任何其他方法吗?
我已经阅读了有关线程的信息,但我真的不知道如何在这里应用它,如果有人可以向我展示如何完成它的示例,我将不胜感激。
它应该是什么样子的快速总结:
同时调用两个 URL,第一个 url 响应决定 python 程序是否继续。
如果 10.10.10.2 可访问,程序将关闭,如果 192.168.100.5 可访问,则程序将继续。
代码示例可能对大家很有帮助。
try:
preflash = urllib.request.urlopen("http://10.10.10.2", timeout=10).getcode()
print("Web page status code:", preflash, "FAIL")
sys.exit(0)
except urllib.error.URLError:
correct = urllib.request.urlopen("http://192.168.100.5", timeout=10).getcode()
print("Web page status code:", correct)
print("IP address: 192.168.100.5 is reachable")
您可以将 aiohttp 与 asyncio 结合使用。
import asyncio
import aiohttp
async def download_file(session: aiohttp.ClientSession, url: str):
async with session.get(url) as response:
assert response.status == 200
# For large files use response.content.read(chunk_size) instead.
return url, await response.read()
async def download_multiple(session: aiohttp.ClientSession):
urls = (
'https://whosebug.com/',
'http://python.org',
)
download_futures = [download_file(session, url) for url in urls]
print('Results')
done, pending = await asyncio.wait(download_futures, return_when=asyncio.FIRST_COMPLETED)
for task in done:
print(task.result())
print("unfinished:", len(pending))
return [p.result()[0] for p in done]
async def main():
async with aiohttp.ClientSession() as session:
result = await download_multiple(session)
print('finished:', result)
asyncio.run(main())
您现在可以玩已完成和待处理的游戏,已完成的包含第一个 url 已完成的,等待其他人。
main() 是协程,运行 by asyncio.run()。 main() 使用 aiohttp 管理异步请求并等待 download_multiple。 download_multiple 创建一个未来列表(等待对象),然后使用 asyncio.wait 来“运行”它们。这里的策略是 return 当第一个 return 时。 asyncio.wait returns 两个任务列表,已完成(已完成)和待处理(未完成)。然后第一个完成,其他的待定。
我正在想办法同时打开两个 URL 以节省时间。 正如您在这里看到的那样,如果 10.10.10.2 可以访问,则程序将关闭,如果 192.168.100.5 可以访问,则程序将继续。 对于这个业余问题,我很抱歉,我对编程很陌生,所以请放轻松。 我下面的解决方案需要很多时间,因为它一个接一个地发生。 我正在 python 中编写一个小型检查工具来查看设备是否具有正确的 IP 地址。所以 10.10.10.2 的设备有故障,这是正确的 IP 地址 192.168.100.5。 有超过 8000 台设备需要测试,因此为每个设备等待 10 秒会花费太多时间。 你们知道同时调用 2 个不同 URL 的任何其他方法吗? 我已经阅读了有关线程的信息,但我真的不知道如何在这里应用它,如果有人可以向我展示如何完成它的示例,我将不胜感激。 它应该是什么样子的快速总结: 同时调用两个 URL,第一个 url 响应决定 python 程序是否继续。 如果 10.10.10.2 可访问,程序将关闭,如果 192.168.100.5 可访问,则程序将继续。
代码示例可能对大家很有帮助。
try:
preflash = urllib.request.urlopen("http://10.10.10.2", timeout=10).getcode()
print("Web page status code:", preflash, "FAIL")
sys.exit(0)
except urllib.error.URLError:
correct = urllib.request.urlopen("http://192.168.100.5", timeout=10).getcode()
print("Web page status code:", correct)
print("IP address: 192.168.100.5 is reachable")
您可以将 aiohttp 与 asyncio 结合使用。
import asyncio
import aiohttp
async def download_file(session: aiohttp.ClientSession, url: str):
async with session.get(url) as response:
assert response.status == 200
# For large files use response.content.read(chunk_size) instead.
return url, await response.read()
async def download_multiple(session: aiohttp.ClientSession):
urls = (
'https://whosebug.com/',
'http://python.org',
)
download_futures = [download_file(session, url) for url in urls]
print('Results')
done, pending = await asyncio.wait(download_futures, return_when=asyncio.FIRST_COMPLETED)
for task in done:
print(task.result())
print("unfinished:", len(pending))
return [p.result()[0] for p in done]
async def main():
async with aiohttp.ClientSession() as session:
result = await download_multiple(session)
print('finished:', result)
asyncio.run(main())
您现在可以玩已完成和待处理的游戏,已完成的包含第一个 url 已完成的,等待其他人。
main() 是协程,运行 by asyncio.run()。 main() 使用 aiohttp 管理异步请求并等待 download_multiple。 download_multiple 创建一个未来列表(等待对象),然后使用 asyncio.wait 来“运行”它们。这里的策略是 return 当第一个 return 时。 asyncio.wait returns 两个任务列表,已完成(已完成)和待处理(未完成)。然后第一个完成,其他的待定。