【问题标题】:Python3 googleads.oauth2 module does not use proxy. Python2 doesPython3 googleads.oauth2 模块不使用代理。 Python2 可以
【发布时间】:2016-02-03 14:15:00
【问题描述】:

我有以下 Python 代码 sn-p 以连接到 Google DFP 广告管理系统。它在 Python2 和 Python3 中运行良好。但是,当使用(Squid)代理时,它不适用于 Python3,因为在使用 Python3 googleads 库时,对 accounts.google.com 的调用会绕过代理。

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

而且我不会明确调用accounts.google.com,这是由Google googleads 库完成的。 pip install googleads

我怀疑 googleads.oauth2 模块是罪魁祸首。这是一个代码sn-p:

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 连接 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 连接 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 连接 ads.google.com:443 - HIER_DIRECT/2a00:1450:4013:c00::64 - 1454507118.952 612 ::1 TCP_MISS/200 196015 连接 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 -

【问题讨论】:

    标签: python oauth double-click squid


    【解决方案1】:

    罪魁祸首不是 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。

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 2013-11-07
      • 2013-05-18
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多