gpt4 book ai didi

error "No more data is available." when trying to connect and disconnect from a Wi-FI using python(错误“没有更多的数据可用。”尝试使用PYTHON连接和断开Wi-Fi时)

转载 作者:bug小助手 更新时间:2023-10-25 13:06:58 29 4
gpt4 key购买 nike



i want to write a code that can connect, disconnect and check if internet is available using the following python code

我想写一个代码,可以连接,断开连接,并检查互联网是否可用使用以下的Python代码


import subprocess
import requests
import time

def disconnect_wifi():
"""Disconnects the current Wi-Fi connection on Windows."""
try:
# Disable the Wi-Fi adapter.
subprocess.run(["netsh", "interface", "set", "interface", "name='Wi-Fi'", "admin=disable"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error disconnecting Wi-Fi: {str(e)}")

def connect_to_wifi(ssid, password):
"""Connects to the specified Wi-Fi network on Windows."""
try:
# Enable the Wi-Fi adapter.
subprocess.run(["netsh", "interface", "set", "interface", "name='Wi-Fi'", "admin=enable"], check=True)

# Connect to the Wi-Fi network using the provided SSID and password.
subprocess.run(["netsh", "wlan", "connect", "ssid", ssid, "name", ssid, "keyMaterial", password], check=True)
except subprocess.CalledProcessError as e:
print(f"Error connecting to Wi-Fi: {str(e)}")

def check_internet_access():
"""Checks if internet access is available."""
try:
# Try to connect to a website.
requests.get("https://google.com")
return True
except requests.exceptions.ConnectionError:
return False

def main():
ssid = "Wifi name" # Replace with your SSID
password = "Wifi passord" # Replace with your password

# Disconnect the current Wi-Fi connection.
disconnect_wifi()
time.sleep(2) # Wait for the adapter to disable.

# Connect to the specified Wi-Fi network.
connect_to_wifi(ssid, password)
time.sleep(5) # Wait for the adapter to connect.

# Check if internet access is available.
if check_internet_access():
print("Internet access is available.")
else:
print("Internet access is not available.")

if __name__ == "__main__":
main()

but every time i run the code i get

但每次我运行代码时我都会收到


###########
No more data is available.

Error disconnecting Wi-Fi: Command '['netsh', 'interface', 'set', 'interface', "name='Wi-Fi'", 'admin=disable']' returned non-zero exit status 1.

No more data is available.


Error connecting to Wi-Fi: Command '['netsh', 'interface', 'set', 'interface', "name='Wi-Fi'", 'admin=enable']' returned non-zero exit status 1.

Internet access is available.

it does not disconnect from a Wi-Fi
does not connect to a Wi-Fi
but it manages to accurately tell if internet is available or not

它不会断开Wi-Fi连接,也不会连接到Wi-Fi,但它会设法准确地判断互联网是否可用


i tried

我试过了


to Restart my computer.
Check your Wi-Fi adapter driver. made sure that it is up to date.
Reset my network settings.

重新启动我的计算机。检查您的Wi-Fi适配器驱动程序。已确保它是最新的。重置我的网络设置。


更多回答

Perhaps if you use subprocess.run and capture the output, etc., instead of os.system, you might get some additional information.

也许,如果您使用subprocess.run并捕获输出等,而不是os.system,您可能会获得一些额外的信息。

error with subprocess.run No more data is available. Error disconnecting Wi-Fi: Command '['netsh', 'interface', 'set', 'interface', "name='Wi-Fi'", 'admin=disable']' returned non-zero exit status 1. No more data is available. Error connecting to Wi-Fi: Command '['netsh', 'interface', 'set', 'interface', "name='Wi-Fi'", 'admin=enable']' returned non-zero exit status 1.

子进程出错。运行没有更多数据可用。断开Wi-Fi时出错:命令‘[’netsh‘,’interface‘,’set‘,’interface‘,“name=’Wi-Fi‘”,’admin=Disable‘]’返回非零退出状态%1。没有更多数据可用。连接Wi-Fi时出错:命令‘[’netsh‘,’interface‘,’set‘,’interface‘,“name=’Wi-Fi‘”,’admin=Enable‘]’返回非零退出状态1。

When you have new info related to your question, you should edit your question and add the info there. Here you should show the new code and any/all output and/or error messages.

当您有与您的问题相关的新信息时,您应该编辑您的问题并在那里添加信息。在这里,您应该显示新代码以及任何/所有输出和/或错误消息。

优秀答案推荐

i found the solution the cmd commands i passed were outdated here is an updated code

我发现我传递的cmd命令的解决方案已过时。以下是更新后的代码


import subprocess
import requests
import time

def disconnect_wifi():
"""Disconnects the current Wi-Fi connection on Windows."""
try:
# Disable the Wi-Fi adapter.
subprocess.run(["netsh", "wlan", "disconnect"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error disconnecting Wi-Fi: {str(e)}")

def connect_to_wifi():
"""Connects to the specified Wi-Fi network on Windows."""
try:

subprocess.run(["netsh", "wlan", "connect", "ssid=HOME","name=HOME"])
except subprocess.CalledProcessError as e:
print(f"Error connecting to Wi-Fi: {str(e)}")

def check_internet_access():
"""Checks if internet access is available."""
try:
# Try to connect to a website.
requests.get("https://google.com")
return True
except requests.exceptions.ConnectionError:
return False

def main():
# Disconnect the current Wi-Fi connection.
disconnect_wifi()
time.sleep(2) # Wait for the adapter to disable.

# Connect to the specified Wi-Fi network.
connect_to_wifi()
time.sleep(5) # Wait for the adapter to connect.

# Check if internet access is available.
if check_internet_access():
print("Internet access is available.")
else:
print("Internet access is not available.")

if __name__ == "__main__":
main()

this code now manages to disconnect and connect to a saved wifi network named HOME and check if internet access is available

这段代码现在可以断开并连接到一个名为home的已保存的wifi网络,并检查互联网访问是否可用


更多回答

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com