How to use http proxy in Google API Python Client?

5,384 views
Skip to first unread message

Cloga

unread,
Mar 2, 2013, 7:59:35 PM3/2/13
to google-api-p...@googlegroups.com
Hi all:
I need use proxy to request Google's services , how I use http proxy with Google API Python Client.I am a fresh man in python.Could some one give me some sample code?Many thanks.
PS:When I use Google API Python Client without proxy , I will get a Errno 10060 of httplib2.


Joe Gregorio

unread,
Mar 3, 2013, 9:27:22 PM3/3/13
to google-api-p...@googlegroups.com
You setup the proxy information when you create the httplib2 object:

http://httplib2.googlecode.com/hg/doc/html/libhttplib2.html#httplib2.Http


>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google API Python Client" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to google-api-python-...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Jeffrey Ng

unread,
Feb 16, 2016, 10:58:43 AM2/16/16
to Google API Python Client
Thanks jcgregorio.Do you mean that, I can do either 1 or 2:

1. Set up my proxy via environment variables e.g. if I am in windows, I can setup my proxy info under control panel-environment variables? 
  • proxy-username with the proxy username
  • proxy-password with the password for the proxy user
  • http_proxy with the url of the proxy to be used for http connections
  • https_proxy with the url of an HTTPS proxy that should be used, note this should start with https://
2. in my python code, write the following lines:

import httplib2
...

p = ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, proxy_host=’http://1111.1111(my proxy)’, proxy_port=8000(my port))
httplib2.Http(proxy_info = p)

Sorry for asking the obvious as I am a newbie and keen user of the google api python client.

On Monday, March 4, 2013 at 10:27:22 AM UTC+8, jcgregorio wrote:
On Sat, Mar 2, 2013 at 7:59 PM, Cloga <clog...@gmail.com> wrote:
> Hi all:
> I need use proxy to request Google's services , how I use http proxy with
> Google API Python Client.I am a fresh man in python.Could some one give me
> some sample code?Many thanks.
> PS:When I use Google API Python Client without proxy , I will get a Errno
> 10060 of httplib2.

You setup the proxy information when you create the httplib2 object:

  http://httplib2.googlecode.com/hg/doc/html/libhttplib2.html#httplib2.Http


>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google API Python Client" group.
> To unsubscribe from this group and stop receiving emails from it, send an
Message has been deleted

Jeffrey Ng

unread,
Feb 17, 2016, 11:49:32 PM2/17/16
to Google API Python Client
it turns out that my corporate policy does not allow 1. but only 2.
For 2. I read that httplib2 does not support proxy, or specifically only support SOCK. What to do if my company supports only http proxy?
Anything we can do about it? If this works, it could enable us to generate more search businesses with Google... I really wonder how the other people connecting to google api via python behind http proxy...

Jeffrey Ng

unread,
Feb 18, 2016, 10:39:37 PM2/18/16
to Google API Python Client

Any clue of fixing?

Here are the codes:

  p = httplib2.ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, proxy_host="proxy without http://", proxy_port='port no', proxy_user=proxyuser, proxy_pass = proxypassword)

  theHttp = httplib2.Http(proxy_info = p)


searchCnter = qGenerator(individual.rstrip(),entity.rstrip(),keyword.rstrip(),searchCnter,theHttp)

....function qGenerator passes theHttp to function gSearch

...function gSearch passes theHttp to build


service = build("customsearch", "v1", developerKey="[dev key]",  http=theHttp)

 ...

 res = service.cse().list(   q= qName,   cx='cx code here',   #lr = lang_en,   num = 10,    ).execute()

...




Here are the error messages:


C:\Users\xxxxx\workspace\Google>python KYCSearch_v0.2.py

build() takes at most 2 positional arguments (3 given)

Traceback (most recent call last):

  File "C:\Python33\lib\site-packages\httplib2-0.9.2-py3.3.egg\httplib2\__init__.py", line 986, in _conn_request

    conn.connect()

  File "C:\Python33\lib\http\client.py", line 1194, in connect

    self.timeout, self.source_address)

  File "C:\Python33\lib\socket.py", line 417, in create_connection

    for res in getaddrinfo(host, port, 0, SOCK_STREAM):

socket.gaierror: [Errno 11004] getaddrinfo failed

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "KYCSearch_v0.2.py", line 208, in <module>

    main()

  File "KYCSearch_v0.2.py", line 198, in main

    searchCnter = qGenerator(individual.rstrip(),'','',searchCnter,theHttp)

  File "KYCSearch_v0.2.py", line 171, in qGenerator

    gSearch(qName_Keywords,str(searchCnter),theHttp)

  File "KYCSearch_v0.2.py", line 102, in gSearch

    http=theHttp)

  File "C:\Python33\lib\site-packages\oauth2client-1.5.0-py3.3.egg\oauth2client\util.py", line 142, in positional_wrapper

  File "C:\Python33\lib\site-packages\google_api_python_client-1.4.2-py3.3.egg\googleapiclient\discovery.py", line 196, in build

  File "C:\Python33\lib\site-packages\google_api_python_client-1.4.2-py3.3.egg\googleapiclient\discovery.py", line 242, in _retrieve_discovery_doc

  File "C:\Python33\lib\site-packages\httplib2-0.9.2-py3.3.egg\httplib2\__init__.py", line 1313, in request

    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)

  File "C:\Python33\lib\site-packages\httplib2-0.9.2-py3.3.egg\httplib2\__init__.py", line 1063, in _request

    (response, content) = self._conn_request(conn, request_uri, method, body, headers)

  File "C:\Python33\lib\site-packages\httplib2-0.9.2-py3.3.egg\httplib2\__init__.py", line 993, in _conn_request

    raise ServerNotFoundError("Unable to find the server at %s" % conn.host)

httplib2.ServerNotFoundError: Unable to find the server at www.googleapis.com

Reply all
Reply to author
Forward
0 new messages