/
rnekrasov
/
Python
Обзор
Документация
Войти
/
rnekrasov
/
Python
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
check_internet_con.py
30 строк
739 B
slowy07
refactor: clean code
30 янв 2022, 04:33
30 янв 2022, 04:33
f0af0c4
Код
Авторство
О чём код?
from sys import argv try: # For Python 3.0 and later from urllib.error import URLError from urllib.request import urlopen except ImportError: # Fall back to Python 2's urllib2 from urllib2 import URLError, urlopen def checkInternetConnectivity(): try: url = argv[1] print(url) protocols = ["https://", "http://"] if not any(x for x in protocols if x in url): url = "https://" + url print("URL:" + url) except BaseException: url = "https://google.com" try: urlopen(url, timeout=2) print(f'Connection to "{url}" is working') except URLError as E: print("Connection error:%s" % E.reason) checkInternetConnectivity()