0

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
Rohith Nambiar
  • 2,957
  • 4
  • 17
  • 37

1 Answers1

1
: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

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Can you explain a bit more on why this works, I am also not sure where I should add this, in my .bat file or vbs file? – Rohith Nambiar Mar 07 '22 at 05:04
  • In your batch script. First, a timeout occurs. `/t 1` means 1 second. The `>nul` suppresses messages to the console. Then `ping` attempts to contact `google.com` & the response is "piped" to `find` instead of to the console. `find` attempts to find the string `TTL` in the response to the `ping`, which will occur if the `ping`ed URL responds. If the response does not include the string `TTL` then no contact was made, so the connection has not yet been established. `find` sets `errorlevel` to `0` if the string was found and `1` otherwise, so if the errorlevel is 1, go back & wait again. – Magoo Mar 07 '22 at 06:10
  • This worked but the script requires a microphone to get input from the user, when I double click on the .vbs file it works but on startup it's not able to access the microphone. – Rohith Nambiar Mar 11 '22 at 05:51