PythonAnywhere 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/", line 14, in <module> client = gspread.authorize(creds) File "/home/MichaelF91/.local/lib/python3.6/site-packages/gspread/", line 402, in authorize client.login() File "/home/MichaelF91/.local/lib/python3.6/site-packages/gspread/", line 61, in login self.auth.refresh(http) File "/usr/local/lib/python3.6/dist-packages/oauth2client/", line 541, in refresh self._refresh(http) File "/usr/local/lib/python3.6/dist-packages/oauth2client/", line 744, in _refresh self._do_refresh_request(http) File "/usr/local/lib/python3.6/dist-packages/oauth2client/", line 775, in _do_refresh_request body=body, headers=headers) File "/usr/local/lib/python3.6/dist-packages/oauth2client/", line 282, in request connection_type=connection_type) File "/usr/local/lib/python3.6/dist-packages/httplib2/", 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/", line 1072, in _request (response, content) = self._conn_request(conn, request_uri, method, body, headers) File "/usr/local/lib/python3.6/dist-packages/httplib2/", line 995, in _conn_request conn.connect() File "/usr/lib/python3.6/http/", line 1392, in connect super().connect() File "/usr/lib/python3.6/http/", line 936, in connect (,self.port), self.timeout, self.source_address) File "/usr/lib/python3.6/", line 722, in create_connection raise err File "/usr/lib/python3.6/", 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

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.