码迷,mamicode.com
首页 > 编程语言 > 详细

python获取http状态码

时间:2020-05-24 09:15:14      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:__name__   sleep   import   example   while   获取   超过   int   code   

获取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()

python获取http状态码

标签:__name__   sleep   import   example   while   获取   超过   int   code   

原文地址:https://blog.51cto.com/liuxiaolan/2497765

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!