cji*_*cji 6 python salesforce suds
我正在尝试使用salesforce-python-toolkit对Salesforce API进行Web服务调用,但是我无法让客户端通过代理.由于工具包是基于suds的,我尝试使用suds本身来看看我是否可以让它尊重那里的代理设置,但它也没有用.
这在OS X 10.7(python 2.7)和ubuntu 12.04上的泡沫0.3.9上进行了测试.
我做的一个示例请求最终没有通过代理(只是在本地运行的burp或charles代理):
import suds
ws = suds.client.Client('file://sandbox.xml',proxy={'http':'http://localhost:8888'})
ws.service.login('user','pass')
Run Code Online (Sandbox Code Playgroud)
我用代理尝试了各种各样的东西 - 使用FQDN丢弃http://,使用IP.我已经逐步完成了pdb中的代码并看到它设置了代理选项.我还尝试在没有代理的情况下实例化客户端,然后使用以下命令设置它:ws.set_options(proxy = {'http':'http:// localhost:8888'})
肥皂水不再使用代理吗?我没有看到它直接列在这里http://jortel.fedorapeople.org/suds/doc/suds.options.Options-class.html,但我确实在运输中看到它.我是否需要通过运输设置不同的方式?当我走进pdb时,看起来确实是在使用传输,但我不确定如何.
谢谢!
cji*_*cji 14
我在freenode上进入了#suds,Xelnor/rbarrois提供了一个很好的答案!显然,suds中的自定义映射会覆盖urllib2使用系统配置环境变量的行为.此解决方案现在依赖于相应地设置http_proxy/https_proxy/no_proxy环境变量.
我希望这可以帮助其他任何人遇到代理和suds(或其他使用suds的库)的问题.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())
Run Code Online (Sandbox Code Playgroud)