python - Python how to continue for loop after 5 second
问题描述
import commands
f = open("test.txt","r").readlines()
for l in f:
x = l.strip()
url = ("https://"+x+"/test")
c = commands.getoutput("curl -I "+url)
print (c)
When the code is executed, the code takes a long time in this line [c = commands.getoutput("curl -I "+url)], I want to set a time for example 5 seconds. If it is longer than 5 seconds, move to the next line in the9 for loop)
解决方案
您也可以使用requests
curl 来处理响应超时:
http ://docs.python-requests.org/en/master/user/quickstart/#timeouts 。像这样的东西:
import requests
from requests.exceptions import Timeout
import commands
f = open("test.txt","r").readlines()
for l in f:
x = l.strip()
url = ("https://"+x+"/test")
try:
response = requests.get(url, timeout=5)
except Timeout:
# do something
continue
推荐阅读
- javascript - 我无法发送指定消息
- ruby-on-rails - 我想批准提出的意见。Ruby On Rails
- python - 堆栈和队列亚马逊面试问题
- python - 尝试在熊猫数据框中将列设置为索引时出错
- python - 如何使用 Web API 在 Odoo 中注册发票付款
- firebase - Xamarin Android Linker - 删除 Firebase 消息传递类
- latex - 我可以在 Math.NET 中获得带有显式打印乘号的 LaTeX 输出吗?
- c# - 如何删除实时图表折线图上的线阴影?
- pyspark - 创建一个火花数据框列由一个列表作为数据类型
- nginx - 如何在 LUA 中使用私钥签署 JWT