Python3 googleads.oauth2 模块不使用代理。 Python2 确实
Python3 googleads.oauth2 module does not use proxy. Python2 does
我有以下 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 -
罪魁祸首不是 googleads.oauth2 模块。这是 HTTPLib2 library. It seems that the HTTPLib2 does not pick up the proxy settings correctly as described in numerous places, for example here: 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。
我有以下 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 -
罪魁祸首不是 googleads.oauth2 模块。这是 HTTPLib2 library. It seems that the HTTPLib2 does not pick up the proxy settings correctly as described in numerous places, for example here: 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。