Python判斷PC是否連網

代碼很簡單,須要設置能訪問的url便可socket

import socketurl

def is_connected(url):code

try:
    host = socket.gethostbyname(url)
    s = socket.create_connection((host, 80), 2)
    return True
except Exception as e:
    return False

URL = "www.baidu.com"get

print is_connected(URL )io

相關文章
相關標籤/搜索