簡體   English   中英

如何在python中檢查用戶是否連接到互聯網?

[英]How to check if user is connected to internet in python?

我是 python 3.5 並使用 urllib3,我看過各種示例,但它們是 urllib2,這就是我問這個問題的原因,我有一些需要互聯網的代碼,但如果用戶未連接,我想向他們顯示設備是沒有連接到互聯網,所以我該怎么做。

您可以執行類似的操作,檢查與站點的連接。

import urllib.request 
import urllib.parse 

try: 
    x = urllib.request.urlopen('https://www.google.com')        
except Exception as e: 
    print(str(e)) 

我以前沒有使用過 urllib,但您可以嘗試以下操作:

try:
    #you put your code here
    #and it will raise an exception if
    #network connection is not available
    #so you catch that
except:
    #your warning code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM