在 python 脚本启动时处理互联网异常
Handle internet exception on python script startup
我想要一个 python 脚本在启动时执行,为此,我创建了一个 bat 文件和一个 vbs 文件
它在有互联网的情况下工作,但大多数时候当我打开电脑并登录时,脚本在电脑连接到互联网之前启动并且它给我这个错误:
Traceback (most recent call last):
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\util\connection.py", line 72, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\ACERq\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 358, in connect
conn = self._new_conn()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000020F127D5FD0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
Traceback (most recent call last):
File "d:\Python-projects\Personal_assistant\Friday.py", line 5, in <module>
import pywhatkit as kit
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\__init__.py", line 16, in <module>
from pywhatkit.whats import (
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\whats.py", line 13, in <module>
core.check_connection()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\core\core.py", line 41, in check_connection
raise InternetException(pywhatkit.core.exceptions.InternetException: Error while connecting to the Internet. Make sure you are connected to the Internet!
我也在为依赖项使用虚拟环境。有什么办法可以处理这些异常,或者只有在连接到互联网后才能执行 python 脚本。如有任何帮助,我们将不胜感激!
.bat 文件
@echo off
python D:\Python-projects\Personal_assistant\Friday.py
@pause
.vbs 文件
CreateObject("Wscript.Shell").Run "D:\Python-projects\Personal_assistant\Friday.bat", 0 , True
:wait
timeout /t 1 >nul
ping -n 1 google.com|find "TTL">nul
if errorlevel 1 goto wait
应该每秒 ping google.com
,直到收到包含 TTL
的响应
我想要一个 python 脚本在启动时执行,为此,我创建了一个 bat 文件和一个 vbs 文件 它在有互联网的情况下工作,但大多数时候当我打开电脑并登录时,脚本在电脑连接到互联网之前启动并且它给我这个错误:
Traceback (most recent call last):
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\util\connection.py", line 72, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\ACERq\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 358, in connect
conn = self._new_conn()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000020F127D5FD0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
Traceback (most recent call last):
File "d:\Python-projects\Personal_assistant\Friday.py", line 5, in <module>
import pywhatkit as kit
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\__init__.py", line 16, in <module>
from pywhatkit.whats import (
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\whats.py", line 13, in <module>
core.check_connection()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\core\core.py", line 41, in check_connection
raise InternetException(pywhatkit.core.exceptions.InternetException: Error while connecting to the Internet. Make sure you are connected to the Internet!
我也在为依赖项使用虚拟环境。有什么办法可以处理这些异常,或者只有在连接到互联网后才能执行 python 脚本。如有任何帮助,我们将不胜感激!
.bat 文件
@echo off
python D:\Python-projects\Personal_assistant\Friday.py
@pause
.vbs 文件
CreateObject("Wscript.Shell").Run "D:\Python-projects\Personal_assistant\Friday.bat", 0 , True
:wait
timeout /t 1 >nul
ping -n 1 google.com|find "TTL">nul
if errorlevel 1 goto wait
应该每秒 ping google.com
,直到收到包含 TTL