【问题标题】:Gspread, PythonAnywhere: "OSError: [Errno 101] Network is unreachable" - Trying to Connect to Google SheetsGspread,PythonAnywhere:“OSError:[Errno 101] 网络无法访问” - 尝试连接到 Google 表格
【发布时间】:2017-06-27 18:02:04
【问题描述】:

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

这段代码在我的本地机器上运行良好,但在 PythonAnywhere 上却失败了OSError: [Errno 101] Network is unreachable

知道如何解决吗?在我尝试运行它的 10 次中,它确实有效。我已经尝试过控制台和 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 

【问题讨论】:

    标签: python google-sheets-api pythonanywhere gspread


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      “即使使用相同的代码也会发生这种情况——也就是说,完全相同的代码可能在某一天有效,然后在下一天无效。不幸的是,我们知道的唯一解决方法是切换到付费帐户。”

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-19
        • 2021-04-11
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 2020-01-06
        • 2020-01-07
        相关资源
        最近更新 更多