测试python是否可以联网

使用如下代码即可 



import requests

# 设置代理服务器地址
proxy_address = "http://127.0.0.1:10809"

# 创建代理字典
proxies = {
    "http": proxy_address,
    "https": proxy_address
}

def check_internet_connection():
    baidu_result = False
    google_result = False

    try:
        # Check connection to baidu.com
        response = requests.get("http://www.baidu.com", proxies=proxies)
        if response.status_code == 200:
            baidu_result = True
    except requests.exceptions.RequestException:
        baidu_result = False

    try:
        # Check connection to google.com
        response = requests.get("http://www.google.com", proxies=proxies)
        if response.status_code == 200:
            google_result = True
    except requests.exceptions.RequestException:
        google_result = False

    if baidu_result:
        print("网络可以上网")
    else:
        print("无法连接到网络")

    if google_result:
        print("已连接谷歌")
    else:
        print("无法科学上网")

# 循环调用函数
for _ in range(5):
    check_internet_connection()

评论

此博客中的热门博文

使用自己的github加速源

量化指标计算

申请证书

搭建属于自己的vpn节点—科学上网

wsl安装cuda

解决v2rayN的延迟是-1的问题

wsl虚拟机释放文件

wsl安装cuda10.2

WSL怎么使用本机进行快速克隆github代码