Forums

Setting up proxy

Hello. I am making a web application that needs to make calls to the google drive api. However, since I use the free account, I need to do it through the proxy. It goes like this:

from apiclient.discovery import build
from httplib2 import Http, ProxyInfo
from oauth2client import file, client, 
from httplib2 import socks

service = build('drive', 'v3', http=creds.authorize(Http()))

I'm pretty sure I need to change the Http() part so that it can use the proxy, but I have no idea how to do it.

Hmm, it looks like httplib2 extracts its proxy settings from the environment by default, which means that it should pick everything up properly -- so this should work:

from apiclient.discovery import build
from httplib2 import Http, ProxyInfo
from oauth2client import file, client, 
from httplib2 import socks

service = build('drive', 'v3', http=Http())

Could you give that a go and see if it works?

No, it didn't work. I get this in my error log:

2018-08-13 17:15:39,480: Error running WSGI application
2018-08-13 17:15:39,482: OSError: [Errno 101] Network is unreachable
2018-08-13 17:15:39,482:   File "/var/www/serverdrive_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-08-13 17:15:39,482:     from serverdrive.site import app as application  # noqa
2018-08-13 17:15:39,482: 
2018-08-13 17:15:39,482:   File "./serverdrive/site.py", line 41, in <module>
2018-08-13 17:15:39,482:     service = build('drive', 'v3', http=creds.authorize(Http()))
2018-08-13 17:15:39,482: 
2018-08-13 17:15:39,483:   File "/usr/local/lib/python3.6/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
2018-08-13 17:15:39,483:     return wrapped(*args, **kwargs)
2018-08-13 17:15:39,483: 
2018-08-13 17:15:39,483:   File "/usr/local/lib/python3.6/dist-packages/googleapiclient/discovery.py", line 228, in build
2018-08-13 17:15:39,483:     requested_url, discovery_http, cache_discovery, cache)
2018-08-13 17:15:39,483: 
2018-08-13 17:15:39,483:   File "/usr/local/lib/python3.6/dist-packages/googleapiclient/discovery.py", line 275, in _retrieve_discovery_doc
2018-08-13 17:15:39,483:     resp, content = http.request(actual_url)
2018-08-13 17:15:39,483: 
2018-08-13 17:15:39,483:   File "/usr/local/lib/python3.6/dist-packages/oauth2client/transport.py", line 175, in new_request
2018-08-13 17:15:39,484:     redirections, connection_type)
2018-08-13 17:15:39,484: 
2018-08-13 17:15:39,484:   File "/usr/local/lib/python3.6/dist-packages/oauth2client/transport.py", line 282, in request
2018-08-13 17:15:39,484:     connection_type=connection_type)
2018-08-13 17:15:39,484: 
2018-08-13 17:15:39,484:   File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 1322, in request
2018-08-13 17:15:39,484:     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
2018-08-13 17:15:39,484: 
2018-08-13 17:15:39,484:   File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 1072, in _request
2018-08-13 17:15:39,485:     (response, content) = self._conn_request(conn, request_uri, method, body, headers)
2018-08-13 17:15:39,485: 
2018-08-13 17:15:39,485:   File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 995, in _conn_request
2018-08-13 17:15:39,485:     conn.connect()
2018-08-13 17:15:39,485: 
2018-08-13 17:15:39,485:   File "/usr/lib/python3.6/http/client.py", line 1392, in connect
2018-08-13 17:15:39,485:     super().connect()
2018-08-13 17:15:39,485: 
2018-08-13 17:15:39,485:   File "/usr/lib/python3.6/http/client.py", line 936, in connect
2018-08-13 17:15:39,486:     (self.host,self.port), self.timeout, self.source_address)
2018-08-13 17:15:39,486: 
2018-08-13 17:15:39,486:   File "/usr/lib/python3.6/socket.py", line 722, in create_connection
2018-08-13 17:15:39,486:     raise err
2018-08-13 17:15:39,486: 
2018-08-13 17:15:39,486:   File "/usr/lib/python3.6/socket.py", line 713, in create_connection
2018-08-13 17:15:39,486:     sock.connect(sa)

OK. So let's try setting the proxy explicitly from the environment:

from apiclient.discovery import build
from httplib2 import Http, ProxyInfo
from oauth2client import file, client, 
from httplib2 import socks
from urllib.parse import urlparse

proxy_netloc = urlparse(os.environ.get("http_proxy")).netloc.split[":"]
service = build('drive', 'v3', http=Http(proxy_info=httplib2.ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, proxy_host=proxy_netloc[0], proxy_port=int(proxy_netloc[1])))

where to define it

In the code where the original code that you posted was.