python httpステータスコードの取得
1045 ワード
httpステータスコードを取得し、戻りステータスコードが200でない場合は5秒おきに再取得しますが、3回を超えません
#!/bin/env python
# -*- coding: UTF-8 -*-
# Author:
# example: python http_status.py ota_list
import requests
import time
import sys
from threading import Thread
def http_status(arg):
try:
html = requests.get(arg)
codes = html.status_code
a = 0
while True:
if codes == 200:
print("t0", codes)
return codes
else:
a = a+1
time.sleep(5)
if a >= 3:
print(codes)
return codes
except:
sys.exit(0)
if __name__ == "__main__":
code = sys.argv[1]
http_url = {
"baidu": "www.baidu.com",
"tmail": "https://www.tmail.com"
}
for i in http_url.keys():
if code in i:
http_url = http_url.get(i)
t = Thread(target=http_status, args=(http_url,))
t.start()