Python3 googleads.oauth2模块不使用代理。 Python2有

时间:2016-02-03 14:15:00

标签: python oauth double-click squid

我有以下几段Python代码可以连接到Google Doubleclick for Publishers。它在Python2和Python3中工作正常。但是,当使用(Squid)代理时,它不适用于Python3,因为在使用Python3 googleads库时,对accounts.google.com的调用会绕过代理。

所以我的问题是为什么对accounts.google.com的调用会绕过代理。

我没有明确调用accounts.google.com,这是由Google googleads库完成的。 pip install googleads

我怀疑googleads.oauth2模块是罪魁祸首。这是一段代码:

from googleads import dfp
    from googleads import oauth2
    import httplib2

    oauth2_client = None
    try:

        proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, <proxy.host>,<proxy.port>)

        oauth2_client = (
            oauth2.GoogleRefreshTokenClient(<dfp.client_id>, <dfp.client_secret>,
                                            <dfp.refresh_token>, proxy_info=proxy_info
            )
        )
    except Exception as e:
        logger.critical("Could not init oauth client", e)

    httpsProxyUrl = "http://{}:{}".format(<proxy.host>,<proxy.port>


    self.dfp_client = dfp.DfpClient(oauth2_client, <dfp.application_name>,
                                    network_code=<dfp.network_code>,
                                    https_proxy=httpsProxyUrl, cache=None)

当使用Python2运行时,Squid日志显示:

  

1454506480.333 788 :: 1 TCP_MISS / 200 399986 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 8b -   1454506480.737 236 :: 1 TCP_MISS / 200 4767 CONNECT 173.194.65.84:443 - HIER_DIRECT / 173.194.65.84 -   1454506487.143 6399 :: 1 TCP_MISS / 200 900716 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 8b -   1454506492.123 1049 :: 1 TCP_MISS / 200 195254 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 8b -   1454506494.129 1928 :: 1 TCP_MISS / 200 7579 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 8b -

所有ads.google.com连接,这都很好。和173.194.65.84的一个连接是accounts.google.com,这也很好......我想,因为我希望有DNS名称,而不是IP地址。奇怪。

使用Python3运行时,我的防火墙会注意到对account.google.com的访问权限。这是不好,因为它绕过了代理。 ads.google.com的流量仍然通过代理:

Squid日志显示了ads.google.com的访问权限。哪个好,但accounts.google.com已不复存在:

  

1454507105.115 924 :: 1 TCP_MISS / 200 401298 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 64 -   1454507114.449 6664 :: 1 TCP_MISS / 200 903366 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 64 -   1454507118.952 612 :: 1 TCP_MISS / 200 196015 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 64 -   1454507120.411 1391 :: 1 TCP_MISS / 200 7909 CONNECT ads.google.com:443 - HIER_DIRECT / 2a00:1450:4013:c00 :: 64 -

1 个答案:

答案 0 :(得分:0)

不是googleads.oauth2模块是罪魁祸首。它是HTTPLib2库。似乎HTTPLib2没有像许多地方所描述的那样正确地获取代理设置,例如:HTTP Proxy ignored in Python 3.4

我通过代理所有HTTP并创建IP地址白名单来绕过代理解决了我的直接问题。我用这个socks代替了。

比代码变为:

from googleads import dfp
from googleads import oauth2
import httplib2
import roaldsocks # socks rewrite

oauth2_client = None
try:
    roaldsocks.setdefaultproxy(roaldsocks.PROXY_TYPE_HTTP <proxy.host>,<proxy.port>)
    roaldsocks.wrapmodule(httplib2)

    oauth2_client = (
        oauth2.GoogleRefreshTokenClient(<dfp.client_id>, <dfp.client_secret>,<dfp.refresh_token>
        )
    )
except Exception as e:
    logger.critical("Could not init oauth client", e)

然后一切都通过代理。如果要排除范围,可以在上述袜子替换中的create_connection方法中添加一些代码。类似的东西:

            if ipaddress.IPv4Address(sa[0]).is_private or \
                        ipaddress.IPv4Address(sa[0]) in ipaddress.IPv4Network('<some range>'):
            sock = _orgsocket(af, socktype, proto) # set original socket
        else:
            sock = socksocket(af, socktype, proto)

请注意,这只适用于ipv4。