我正在尝试使用 salesforce-python-toolkit 对 Salesforce API 进行 Web 服务调用,但是我在让客户端通过代理时遇到问题。由于该工具包基于 suds 之上,因此我尝试只使用 suds 本身,看看是否可以让它尊重那里的代理设置,但它也不起作用。
这是在 OS X 10.7 (python 2.7) 和 ubuntu 12.04 上的 suds 0.3.9 上进行测试的。
我发出的一个示例请求最终没有通过代理(只是在本地运行的 burp 或 charles 代理):
import suds
ws = suds.client.Client('file://sandbox.xml',proxy={'http':'http://localhost:8888'})
ws.service.login('user','pass')
我已经尝试过使用代理的各种方法 - 删除 http://、使用 IP、使用 FQDN。我已经浏览了 pdb 中的代码并看到它设置了代理选项。我还尝试在没有代理的情况下实例化客户端,然后使用以下命令进行设置:
ws.set_options(proxy={'http':'http://localhost:8888'})
suds不再使用代理了吗?我没有看到它直接列在这里http://jortel.fedorapeople.org/suds/doc/suds.options.Options-class.html http://jortel.fedorapeople.org/suds/doc/suds.options.Options-class.html,但我确实看到它正在运输中。我需要通过传输进行不同的设置吗?当我在 pdb 中单步执行时,它看起来确实像是在使用传输,但我不确定如何使用。
谢谢你!
我进入了 freenode 上的#suds,Xelnor/rbarrois 提供了一个很好的答案!显然,suds 中的自定义映射会覆盖 urllib2 使用系统配置环境变量的行为。该解决方案现在依赖于相应设置的 http_proxy/https_proxy/no_proxy 环境变量。
我希望这可以帮助其他遇到代理和 suds(或其他使用 suds 的库)问题的人。https://gist.github.com/3721801 https://gist.github.com/3721801
from suds.transport.http import HttpTransport as SudsHttpTransport
class WellBehavedHttpTransport(SudsHttpTransport):
"""HttpTransport which properly obeys the ``*_proxy`` environment variables."""
def u2handlers(self):
"""Return a list of specific handlers to add.
The urllib2 logic regarding ``build_opener(*handlers)`` is:
- It has a list of default handlers to use
- If a subclass or an instance of one of those default handlers is given
in ``*handlers``, it overrides the default one.
Suds uses a custom {'protocol': 'proxy'} mapping in self.proxy, and adds
a ProxyHandler(self.proxy) to that list of handlers.
This overrides the default behaviour of urllib2, which would otherwise
use the system configuration (environment variables on Linux, System
Configuration on Mac OS, ...) to determine which proxies to use for
the current protocol, and when not to use a proxy (no_proxy).
Thus, passing an empty list will use the default ProxyHandler which
behaves correctly.
"""
return []
client = suds.client.Client(my_wsdl, transport=WellBehavedHttpTransport())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)