Suds 忽略代理设置 [英] Suds ignoring proxy setting

查看:61
本文介绍了Suds 忽略代理设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 salesforce-python-toolkit 对 Salesforce API 进行 Web 服务调用,但是我无法让客户端通过代理.由于该工具包基于 suds,因此我尝试向下仅使用 suds 本身,看看我是否可以让它尊重那里的代理设置,但它也不起作用.

I'm trying to use the salesforce-python-toolkit to make web services calls to the Salesforce API, however I'm having trouble getting the client to go through a proxy. Since the toolkit is based on top of suds, I tried going down to use just suds itself to see if I could get it to respect the proxy setting there, but it didn't work either.

这已在 OS X 10.7 (python 2.7) 和 ubuntu 12.04 上的 suds 0.3.9 上进行了测试.

This is tested on suds 0.3.9 on both OS X 10.7 (python 2.7) and ubuntu 12.04.

我提出的一个示例请求最终没有通过代理(只是在本地运行的 burp 或 charles 代理):

an example request I've made that did not end up going through the proxy (just burp or charles proxy running locally):

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'})

I've tried various things with the proxy - dropping http://, using an IP, using a FQDN. I've stepped through the code in pdb and see it setting the proxy option. I've also tried instantiating the client without the proxy and then setting it with: ws.set_options(proxy={'http':'http://localhost:8888'})

suds 不再使用代理了吗?我没有看到它直接列在这里 http://jortel.fedorapeople.org/suds/doc/suds.options.Options-class.html,但我确实在传输中看到了它.我是否需要通过传输进行不同的设置?当我进入 pdb 时,它确实看起来像是在使用传输,但我不确定如何.

Is proxy not used by suds any longer? I don't see it listed directly here http://jortel.fedorapeople.org/suds/doc/suds.options.Options-class.html, but I do see it under transport. Do I need to set it differently through a transport? When I stepped through in pdb it did look like it was using a transport, but I'm not sure how.

谢谢!

推荐答案

我进入了 freenode 上的 #suds,Xelnor/rbarrois 提供了一个很好的答案!显然 suds 中的自定义映射覆盖了 urllib2 使用系统配置环境变量的行为.此解决方案现在依赖于相应地设置 http_proxy/https_proxy/no_proxy 环境变量.

I went into #suds on freenode and Xelnor/rbarrois provided a great answer! Apparently the custom mapping in suds overrides urllib2's behavior for using the system configuration environment variables. This solution now relies on having the http_proxy/https_proxy/no_proxy environment variables set accordingly.

我希望这可以帮助其他人遇到代理和 suds(或其他使用 suds 的库)问题.https://gist.github.com/3721801

I hope this helps anyone else running into issues with proxies and suds (or other libraries that use 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())

这篇关于Suds 忽略代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆