[Python Code] Python Check Internet
Python Check Internet
Cách check mạng internet On hay Off bằng cách kiểm tra một trang web hợp lệ có đang hoạt động hay không.
Mẫu code python như sau
# phattrienphanmem123az.com try: from urllib.error import URLError from urllib.request import urlopen except ImportError: from urllib2 import URLError, urlopen def CheckInternetOn(link): if len(link) == 0: print("link is invalid") return pro_list = ["https://", "http://"] if not any(s for s in pro_list if s in link): link = "https://" + link try: urlopen(link, timeout=2) print(f'Connection to "{link}" is working') except URLError as E: print("Connection error:%s" % E.reason) print("invalid link or the network is error") def main(): # input valid link CheckInternetOn("https://phattrienphanmem123az.com") if __name__ == "__main__": main()
Ok.