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

i am also getting the same error when converting my html file to pdf . i dont understand why its happening i can send that html using smtp google but can not convert it to pdf. please help? thanx in advance

See my reply on the other thread that you posted to.

Hi ,

I am facing the issue.initially few transactions are working then it is failing for all .whenI am trying to send the mail through standalone script it is working but in the application it is failing.

Traceback (most recent call last):
  File "/home/swainsandeep/.virtualenvs/UIS/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/swainsandeep/.virtualenvs/UIS/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/swainsandeep/.virtualenvs/UIS/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/swainsandeep/.virtualenvs/UIS/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/swainsandeep/.virtualenvs/UIS/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/swainsandeep/.virtualenvs/UIS/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "./main.py", line 142, in welcome
    sendemail(DonerEmail,MailSubject,MailBody,TypeOfEmail,filename)
  File "./main.py", line 1068, in sendemail
    s = smtplib.SMTP('smtp.gmail.com', 587)
  File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.6/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.6/smtplib.py", line 307, in _get_socket
    self.source_address)
  File "/usr/lib/python3.6/socket.py", line 724, 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

Please help....

[edited by admin: formatting]

It seems that gmail recently added some extra IPs that we have not whitelisted yet. We are working on sorting that out right now.

Thanks for the reply.may I know how much time it will take?

That should be sorted now -- could you check and let us know if it's still failing?

<br>hi everybody, <br>script: <br>s = smtplib.SMTP_SSL('smtp.mail.ru', 465) <br>err: <br>OSError: [Errno 101] Network is unreachable <br> <br>can you help me to solve this, please?

Have you tried that in a new console, or reloaded your web app since you upgraded?

I just upgraded my account and it started working.