Home > Back-end >  Handle internet exception on python script startup
Handle internet exception on python script startup

Time:03-07

I want a python script to execute on startup, so for that, I created a bat file and a vbs file and it works when there is internet but most of the time when I turn on the pc and log in, the script launches before the pc connects to the internet and it gives me this error:


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!

I am also using a virtual environment for the dependencies. Is there any way I can handle these exceptions or any way to execute a python script only after getting connected to the internet. Any help would be appreciated!

.bat file

@echo off

python D:\Python-projects\Personal_assistant\Friday.py

@pause

.vbs file

CreateObject("Wscript.Shell").Run "D:\Python-projects\Personal_assistant\Friday.bat", 0 , True

CodePudding user response:

:wait
timeout /t 1 >nul
ping -n 1 google.com|find "TTL">nul
if errorlevel 1 goto wait

Should ping google.com every second until a response is received containing TTL

  • Related