【发布时间】:2015-03-11 09:54:40
【问题描述】:
我正在使用一个 while 循环,我试图运行 5 分钟并检查它是否获得 http 响应代码 200。问题是如果它没有获得 200,它将继续运行,但它没有当站点应该启动并运行时,不要去其他地方。
r = requests.head("http://www.testing.co.uk")
while r.status_code != 200:
print "Response not == to 200."
time.sleep(30)
else:
print "Response is 200 - OK"
【问题讨论】:
-
那么问题出在哪里?
-
除非您重复请求,否则
status_code不会改变! -
当它第一次没有达到 200 时,它会继续循环运行,如果站点在 50 秒后启动并运行。
-
你应该尝试在
while count < 5:这样的while循环中给出尝试次数而不是count -=,而不是无限调用它。
标签: python python-2.7 while-loop python-requests