问题描述
我目前正在运行一个脚本来从 Google Analytics 中提取数据,googleapiclient Python 包(基于 httplib2 客户端对象)
I'm currently running a script to pull data from Google Analytics with googleapiclient Python package (that is based on httplib2 client object)
--> 我的脚本在没有任何代理的情况下完美运行.
但是我必须把它放在我的公司代理后面,所以我需要调整我的 httplib2.Http()
对象来嵌入代理信息.
But I have to put it behind my corporate proxy, so I need to adapt my httplib2.Http()
object to embed proxy information.
遵循 httplib2
doc 1 我试过:
Following httplib2
doc 1 I tried:
pi = httplib2.proxy_info_from_url('http://user:pwd@someproxy:80')
httplib2.Http(proxy_info=pi).request("http://www.google.com")
但是没有用.无论有没有代理信息,我总是收到超时错误(因此不考虑参数中的 proxy_info)
But it did not work.I always get a Time out error, with or without the proxy info (so proxy_info in parameter is not taken into account)
我还在 PySocks
包 (v1.5.6) 中下载了袜子,并尝试包装模块"httplib2,如下所述:https://github.com/jcgregorio/httplib2/issues/205
I also downloaded socks in PySocks
package (v1.5.6) and tried to "wrapmodule" httplib2 as described in here:https://github.com/jcgregorio/httplib2/issues/205
socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, "proxyna", port=80, username='p.tisserand', password='Telematics12')
socks.wrapmodule(httplib2)
h = httplib2.Http()
h.request("http://google.com")
但是我得到一个 IndexError: (tuple index out of range)
But I get an IndexError: (tuple index out of range)
与此同时,当我使用 requests 包时,这个简单的代码完美运行:
In the meantime,When I use the requests package, this simple code works perfectly:
os.environ["HTTP_PROXY"] = "http://user:pwd@someproxy:80"
req = requests.get("http://www.google.com")
问题是需要符合 googleapiclient
要求并提供 htpplib2.Http()
客户端对象.
The problem is that need to fit with googleapiclient
requirements and provide a htpplib2.Http()
client object.
推荐答案
我决定在 Python 2 中重新编码我的 Web 应用程序,仍然使用 httplib2 包.现在考虑代理信息.现在可以使用了.
I decided to recode my web app in Python 2, still using the httplib2 package.Proxy info are now taken into account. It now works.
这篇关于Google API + 代理 + httplib2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!