我希望我能在这里找到一些帮助。在搜索了“检查python是否可用internet连接”的可能性之后,我找到了执行该检查的各种方法。然而,那些建议的方法对我没有用,我也不知道为什么没有,所以我正在寻求一些建议。
设置:
我有一个Pi4并运行Raspap,打开一个Hotspot,我可以通过一个静态IP地址访问它。Raspap被配置为连接到wifi LTE路由器以获得互联网连接。我正在使用这个Pi4在无头模式,并使用Raspap访问Pi或配置一个不同的Wifi网络,而不是wifi LTE路由器。
我有一个运行的python脚本来检查文件夹中的文件,并将它们上传到云服务,如果一个互联网连接可用的话,,,然后重命名这些文件。在我安装Raspap之前,Pi只连接到wifi或者没有连接,并且我的互联网连接检查是有效的。
场景:
移动LTE路由器没有插入SIM卡,因此PI4连接到移动LTE路由器wifi,但没有互联网接入。在这种情况下,python脚本应该认识到,没有互联网连接,也没有上传任何文件。但是,wifi检查的if条件仍然是正确的,当然上传不起作用,但是脚本在之后执行重命名。
用于上传和重命名的python脚本如下所示:
def upload():
# run a function to check a folder for files without a "X_" prefix and put it to an uploadQueue (as an global array)
retrieveFilesForUpload(uploadFolder, 60, olderThanXDays=olderThanXDays)
# if upload queue contains files and the Pi is connected to the internet, then upload the files
if len(uploadQueue)>0 and isConnectedToInternet():
# the retrieveAndUpload function uses the global array as input
upload = threading.Thread(target=retrieveAndUpload)
upload.start()
upload.join()
# after uploading the file, rename it by adding a Prefix "X_"
renameUploadedFiles(uploadFolder)"isConnectedToInternet()“函数看起来像其中的一个(它们在末尾有一个数字来测试不同的方法):
def isConnectedWithInternet0():
try:
socket.create_connection(("1.1.1.1", 53))
return True
except OSError:
pass
return False
def isConnectedWithInternet2():
for timeout in [1, 5, 10, 15]:
print("timeout:", timeout)
try:
socket.setdefaulttimeout(timeout)
host = socket.gethostbyname("www.google.com")
s = socket.create_connection((host, 443), 2)
s.close()
print("Internet connection available")
return True
except OSError:
print("No Internet")
pass
return False
def isConnectedWithInternet():
import requests
timeout = 2
url = "http://8.8.8.8"
try:
request = requests.head(url, timeout=timeout)
print("Function 3: Connected to internet")
return True
except (requests.ConnectionError, requests.Timeout) as exception:
print("Function 3: no Internet")
return False
def isConnectedWithInternet4():
host = "8.8.8.8"
port = 53
result = ""
try:
socket.setdefaulttimeout(2)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
#return True
except socket.error as ex:
print(ex)
return False
else:
s.close()
return True
#return result
def isConnectedWithInternet5():
import urllib.request
try:
urllib.request.urlopen("http://216.58.192.142", timeout=1)
return True
except urllib.request.error as err:
print(err)
return False
def isConnectedWithInternet6():
try:
import httplib
except:
import http.client as httplib
timeout = 2
url = "www.google.com"
conn = httplib.HTTPConnection(url, timeout=timeout)
try:
conn.request("HEAD", "/")
conn.close()
return True
except Exception as e:
print(e)
return False但是,虽然Wifi是连接到一个wifi LTE路由器,没有插入SIM卡(没有互联网连接),我总是得到真实的结果,无论上面我使用什么功能。
我希望有人能在这个问题上给我一个提示/建议/一些帮助。
非常感谢!
最佳Khaniii
发布于 2021-03-26 18:06:06
您可以测试对internet服务器的ping。在监控我的网络连接时,这对我起了作用:
import platform # For getting the operating system name
import subprocess # For executing a shell command
import re
def ping(host):
results = []
param = '-n' if platform.system().lower()=='windows' else '-c'
command = ['ping', param, '1', host]
output = subprocess.Popen(command, stdout=subprocess.PIPE).stdout.read()
result = re.findall("=\ (\d+)ms", str(output))
results.append(result)
return results
ping('8.8.8.8')这将返回列表中的ping值,如果没有ping答案,则返回空列表。
[['6', '6', '6']]
or
[[]]https://stackoverflow.com/questions/66821280
复制相似问题