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
最佳答案
您使用的是预安装在PythonAnywhere上的gspread的较旧版本,它无法识别代理设置,因此无法连接。尝试使用更新版本的gspread。