转载大神的检测网站重定向的python脚本

#!/usr/bin/env python
#coding=utf8
import sys
import requests
def check_for_redirects(url):
    try:
        r =requests.get(url,allow_redirects=False,timeout=0.5)
        if 300 <= r.status_code < 400:
            return r.headers['location']
        else:
            return '[no redirect]'
    except requests.exceptions.Timeout:
        return '[timeout]'
    except requests.exceptions.ConnectionError:
        return '[connection error]'
def check_domains(urls):
    for url in urls:
        url_to_check = url if url.startswith('http') else "http://%s" %url
        redirect_url = check_for_redirects(url_to_check)
        print ("%s => %s" %(url_to_check,redirect_url))
if __name__ == '___main__':
    fname = 'domains.txt'
    try:
        fname = sys.argv[1]
    except IndexError:
        pass
    urls = (l.strip() for l in open(fname).readlines())
    check_domains(urls)

  由于是在微信中看到的,链接也忘了,原作者看到了,请海涵!

posted @ 2016-11-04 00:53  梦轻尘  阅读(498)  评论(0编辑  收藏  举报