ninjahatori
Posts: 1
Joined: Tue Jun 09, 2020 4:14 am

Raspberry pi run startup python script cannot send HTTP requests

Tue Jun 09, 2020 4:21 am

I let my python script run at startup but some of HTTP requests (using requests pip module) not working. However, when I run the script manually with sudo openvt -s -w /path/to/launcher.sh, all HTTP requests are working fine.

this is my code in ~/.bashrc`

Code: Select all

sudo openvt -s -w /path/to/launcher.sh
and in launcher.sh, it is as simple as cd to directory and run python script.

Code: Select all

cd /path/to/python/script
python start.py
when the script is run at startup. HTTP requests throw this error:

Code: Select all

ConnectionError: HTTPSConnectionPool(host='', port=443): Max retries exceeded with url: /my/api (Caused by <class 'socket.gaierror'>: [Errno -3] Temporary failure in name resolution)
Again, when I run the script via ssh using this command sudo openvt -s -w /path/to/launcher.sh, everything is working fine. I'm struggling this bugs for days. Thank you so much for any helps :)

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Raspberry pi run startup python script cannot send HTTP requests

Tue Jun 09, 2020 1:16 pm

How are you running the script at startup? If you are using cron and have not put in a delay, it is almost certainly running before the network is properly set up and initialized, which typically takes a long time by computer standards (a second to a few seconds). The quick and dirty fix is to have the script sleep for 10-20 seconds before trying to access the network. An arguably better fix is to loop on a try...except...finally loop which tests whether a predictably 'up' internet site (many people use google.com) is accessible. If this continues to fail after a reasonable time has elapsed, you know that something has really gone wrong, and can take appropriate action.

klricks
Posts: 7293
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Raspberry pi run startup python script cannot send HTTP requests

Tue Jun 09, 2020 3:55 pm

You might try the wait for network on boot option which you can set using GUI config tool or sudo raspi.config
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Raspberry pi run startup python script cannot send HTTP requests

Tue Jun 09, 2020 4:19 pm

klricks wrote:
Tue Jun 09, 2020 3:55 pm
You might try the wait for network on boot option which you can set using GUI config tool or sudo raspi.config
I have found that (possibly depending on what you are trying to do over the network) this is not always sufficient. I think that 'wait for network on boot' may only wait until the local network hardware is working - it may not wait until all the handshaking involved (particularly) in a WiFi internet connection is completed. It has certainly been my experience that scripts requiring immediate internet access which are triggered on reboot can fail for network-related reasons even if 'wait for network on boot' is set.

Return to “Troubleshooting”