学校的IPV6经常莫名其妙自己断开,所以想到自动判断IPV6的网站是否可以访问,如果监测到网站无法访问了,就自动连一下网。美滋滋。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') requests.packages.urllib3.disable_warnings(InsecureRequestWarning) f = open('url.txt', 'r') url = f.readlines() length = len(url) url_result_success = [] url_result_failed = [] for i in range(0, length): try: response = requests.get(url[i].strip(), verify=False, allow_redirects=True, timeout=5) if response.status_code != 200: raise requests.RequestException(u"Status code error: {}".format(response.status_code)) except requests.RequestException as e: url_result_failed.append(url[i]) continue url_result_success.append(url[i]) f.close() result_len1 = len(url_result_failed) result_len2 = len(url_result_success) for i in range(0, result_len1): print(url_result_failed[i].strip() + "打开失败") print(" ") for j in range(0, result_len2): print(url_result_success[j].strip() + "打开成功") |
url.txt
里面就放入网址列表就行了
1 2 3 4 |
https://www.google.com/ http://bt.neu6.edu.cn/ https://bt.byr.cn/ https://www.baidu.com/ |
本文最后更新于2021年3月15日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!