Forums

Code sometimes works, but sometimes doesn't - [Errno 101] Network is unreachable

Hello all, I'm after a bit of help with getting my code to execute consistently. The code itself is fine - it runs successfully from my desktop, and it has worked once or twice on PythonAnywhere both run directly and as a scheduled task (I'm a free user).

The script simply scrapes Google Finance for the current price of a particular fund, then inputs it into my Google Sheets spreadsheet.

Here is the code

Any thoughts on where I'm going wrong?

The error I've been getting today is this one:

Traceback (most recent call last): File "/home/MichaelF91/PriceTracker/PriceTracker.py", line 14, in <module> client = gspread.authorize(creds) File "/home/MichaelF91/.local/lib/python3.6/site-packages/gspread/client.py", line 402, in authorize client.login() File "/home/MichaelF91/.local/lib/python3.6/site-packages/gspread/client.py", line 61, in login self.auth.refresh(http) File "/usr/local/lib/python3.6/dist-packages/oauth2client/client.py", line 541, in refresh self._refresh(http) File "/usr/local/lib/python3.6/dist-packages/oauth2client/client.py", line 744, in _refresh self._do_refresh_request(http) File "/usr/local/lib/python3.6/dist-packages/oauth2client/client.py", line 775, in _do_refresh_request body=body, headers=headers) File "/usr/local/lib/python3.6/dist-packages/oauth2client/transport.py", line 282, in request connection_type=connection_type) File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 1322, in request (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 1072, in _request (response, content) = self._conn_request(conn, request_uri, method, body, headers) File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 995, in _conn_request conn.connect() File "/usr/lib/python3.6/http/client.py", line 1392, in connect super().connect() File "/usr/lib/python3.6/http/client.py", line 936, in connect (self.host,self.port), self.timeout, self.source_address) File "/usr/lib/python3.6/socket.py", line 722, in create_connection raise err File "/usr/lib/python3.6/socket.py", line 713, in create_connection sock.connect(sa) OSError: [Errno 101] Network is unreachable

I'm a little surprised that that worked at all on a PythonAnywhere free account. Free accounts are only allowed http(s) access to a whitelist of sites. We implement that by passing free requests through a proxy. Some Python http libraries don't accept proxy settings or don't work with the proxy. httplib2 in Python 3 is one of those. See http://help.pythonanywhere.com/pages/403ForbiddenError/

I've run it a few more times - some of the time it works, some of the time it doesn't! I don't understand why it would be so inconsistent. Surely it should be all or nothing.

Is there any way I can get around this short of stumping up for a paid up account? I've literally only started learning Python this week and this is just a silly little project I was trying out. I see on the link you provided, a library such as 'requests' should be able to manage it all on it's own.

Is your code possibly following 2 paths and when it takes one path it works and when it takes the other it fails?

Yes, requests does handle the proxy correctly, but you're not using requests.

A bit of extra information -- there have been reports from other users that the gspread module is unreliable from free accounts on PythonAnywhere. It sounds like sometimes, for reasons known only to its developers, it tries to connect to the network directly, bypassing the proxy. This happens even with the same code -- that is, the exact same code might work one day and then not the next.

Unfortunately the only fix we're aware of is to switch to a paid account.

hi...! i also have facing same error now days. "[Errno 101] Network is unreachable" i am only sending outputs into emails. using sender module in python 2. i am doing this from 2.5 months(i guess) it was working fine anyway. but from couple of days i m facing this issues.. code was same .

Which email servers are you connecting to?

i don't know exactly what are you asking for. sorry. here is the module that i am using for sending mails. http://sender.readthedocs.io/ and with smtp server by passing this parameter host='smtp.gmail.com' so it was Gmail service i guess.

should i change the library for sending the emails?

Yes, that was what I needed to know -- you're using smtp.gmail.com. That should definitely work. Where are you running the code? Is it part of a website, or a scheduled task, or in a console?

it is in a scheduled task. and yup in console also. i tried this many time on console, everything is running fine. but yesterday, task didn't work at all so i tested script using console. and i got this error "[Errno 101] Network is unreachable" four times (approx) . and now again the same code is running fine.

Very odd. It sounds like for some reason in some consoles, our system is giving you an IP address for smtp.gmail.com that isn't in the set of addresses we whitelist for them. If/when it happens again, it would be really useful if you could post back here with the results of running these bash commands:

hostname
dig smtp.gmail.com

Then we should be able to track down what's going on.

i got same error again. with the same script , here is the output for bash script that you told me.

giles-liveconsole2
; <<>> DiG 9.9.5-3ubuntu0.13-Ubuntu <<>> smtp.gmail.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44637
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;smtp.gmail.com.                        IN      A
;; ANSWER SECTION:
smtp.gmail.com.         0       IN      CNAME   gmail-smtp-msa.l.google.com.
gmail-smtp-msa.l.google.com. 50 IN      A       173.194.204.109
gmail-smtp-msa.l.google.com. 50 IN      A       173.194.204.108
;; Query time: 3 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Jul 06 05:42:01 UTC 2018
;; MSG SIZE  rcvd: 113

I just want to add I am also seeing the same error trying to use Flask-Mail to send through Gmail, I have a free account on PythonAnywhere. It worked fine up until the last few days. It would send mail without issue. I would really appreciate any help!

Small recap of the issue, Flask-Mail logs into gmail, sends mail, worked fine for a long time, recently stoped worring with the 'OSError: [Errno 101] Network is unreachable' my code has not changed at all.

I just want to add that 30 seconds after my last post about it not working, I tried again and it seems to work without issue. What gives?

Ok. It looks like google grew some more IP ranges. I have added the new ranges so it should be fine now.

again same error.

giles-liveconsole2
; <<>> DiG 9.9.5-3ubuntu0.13-Ubuntu <<>> smtp.gmail.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62499
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;smtp.gmail.com.                        IN      A
;; ANSWER SECTION:
smtp.gmail.com.         60      IN      CNAME   gmail-smtp-msa.l.google.com.
gmail-smtp-msa.l.google.com. 60 IN      A       173.194.205.109
gmail-smtp-msa.l.google.com. 60 IN      A       173.194.205.108
;; Query time: 4 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Jul 07 05:25:53 UTC 2018
;; MSG SIZE  rcvd: 113

@rahulrmacwan OK -- just to confirm, this is a script that's trying to send email via smtp.gmail.com, right?

The next time it happens, could you try running "ping smtp.gmail.com"? You'll need to hit control-C to interrupt the command when it's running.

Just to add this seemed to have happened on my account again as well.

Is this also with smtp.gmail.com?

again i got this error.... @ giles this is the output for "ping smtp.gmail.com"

 PING gmail-smtp-msa.l.google.com (172.217.197.109) 56(84) bytes of data.
From ip-10-0-0-78.ec2.internal (10.0.0.78) icmp_seq=1 Destination Port Unreachable
From ip-10-0-0-78.ec2.internal (10.0.0.78) icmp_seq=1 Destination Port Unreachable
From ip-10-0-0-78.ec2.internal (10.0.0.78) icmp_seq=1 Destination Port Unreachable
From ip-10-0-0-78.ec2.internal (10.0.0.78) icmp_seq=1 Destination Port Unreachable