Gspread,PythonAnywhere:“OSError:[Errno 101]网络无法访问” - 试图连接到Google表格

时间:2017-06-27 18:02:04

标签: python google-sheets-api pythonanywhere gspread

scope = ['https://spreadsheets.google.com/feeds'] creds = ServiceAccountCredentials.from_json_keyfile_name('NFL Redraft-e021dc0d5969.json', scope) client = gspread.authorize(creds)

此代码在我的本地计算机上运行良好,但在使用OSError: [Errno 101] Network is unreachable

的PythonAnywhere上失败了

知道怎么解决?它试图运行它的10次中有1次工作。我试过了控制台和ipython。 Gspread和Praw是最新的。

OSError                                   Traceback (most recent call last)
<ipython-input-1-148cc5cb6e1b> in <module>()
   1303 players, draft_pick_assignments, ordered_picks, positions = initialize_draft()
   1304 pick_string, round_up, pick_up = pick_up_next()
-> 1305 x, x, x, comments, messages = authenticate_google()
   1306 comments_replied_to = restore_from_sheets(comments)
   1307 messages_processed = restore_from_sheets(messages)
<ipython-input-1-148cc5cb6e1b> in authenticate_google()
     39     scope = ['https://spreadsheets.google.com/feeds']
     40     creds = ServiceAccountCredentials.from_json_keyfile_name('NFL Redraft-e021dc0d5969.json', scope)
---> 41     client = gspread.authorize(creds)
     42 
     43     sht = client.open("Redraft Test")
/home/jj2807/.local/lib/python3.5/site-packages/gspread/client.py in authorize(credentials)
    400     """
    401     client = Client(auth=credentials)
--> 402     client.login()
    403     return client
/home/jj2807/.local/lib/python3.5/site-packages/gspread/client.py in login(self)
     59 
     60             http = httplib2.Http()
---> 61             self.auth.refresh(http)
     62 
     63         self.session.add_header('Authorization', "Bearer " + self.auth.access_token)

/usr/local/lib/python3.5/dist-packages/oauth2client/client.py in refresh(self, http)
    558                   request.
    559         """
--> 560         self._refresh(http.request)
    561 
    562     def revoke(self, http):

/usr/local/lib/python3.5/dist-packages/oauth2client/client.py in _refresh(self, http_request)
    763         """
    764         if not self.store:
--> 765             self._do_refresh_request(http_request)
    766         else:
    767             self.store.acquire_lock()

/usr/local/lib/python3.5/dist-packages/oauth2client/client.py in _do_refresh_request(self, http_request)
    795         logger.info('Refreshing access_token')
    796         resp, content = http_request(
--> 797             self.token_uri, method='POST', body=body, headers=headers)
    798         content = _helpers._from_bytes(content)
    799         if resp.status == http_client.OK:

/usr/local/lib/python3.5/dist-packages/httplib2/__init__.py in request(self, uri, method, body, headers, redirections, connection_type)
   1312                     content = b""
   1313                 else:
-> 1314                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
   1315         except Exception as e:
   1316             if self.force_exception_to_status_code:

/usr/local/lib/python3.5/dist-packages/httplib2/__init__.py in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
   1062             auth.request(method, request_uri, headers, body)
   1063 
-> 1064         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
   1065 
   1066         if auth:

/usr/local/lib/python3.5/dist-packages/httplib2/__init__.py in _conn_request(self, conn, request_uri, method, body, headers)
    985             try:
    986                 if conn.sock is None:
--> 987                     conn.connect()
    988                 conn.request(method, request_uri, body, headers)
    989             except socket.timeout:

/usr/lib/python3.5/http/client.py in connect(self)
   1250             "Connect to a host on a given (SSL) port."
   1251 
-> 1252             super().connect()
   1253 
   1254             if self._tunnel_host:

/usr/lib/python3.5/http/client.py in connect(self)
    847         """Connect to the host and port specified in __init__."""
    848         self.sock = self._create_connection(
--> 849             (self.host,self.port), self.timeout, self.source_address)
    850         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    851 

/usr/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    709 
    710     if err is not None:
--> 711         raise err
    712     else:
    713         raise error("getaddrinfo returns an empty list")

/usr/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    700             if source_address:
    701                 sock.bind(source_address)
--> 702             sock.connect(sa)
    703             return sock
    704 

2 个答案:

答案 0 :(得分:3)

您使用的是PythonAnywhere上预装的旧版gspread,但它无法识别代理设置,因此您无法连接。尝试使用较新版本的gspread。

答案 1 :(得分:1)

很遗憾,您可能需要升级到付费帐户才能解决此问题。根据Python Anywhere工作人员的说法,免费帐户会通过代理将其限制为API白名单。 Gspread显然会尝试通过代理一次,然后绕过它。这是来自另一个线程的工作人员:

&#34;即使使用相同的代码也会发生这种情况 - 也就是说,完全相同的代码可能会在某一天工作,然后不会在下一代工作。不幸的是,我们所知道的唯一解决方法是切换到付费帐户。&#34;

https://www.pythonanywhere.com/forums/topic/11822/