0

对 Python 来说还是很新的。我有一个类似于这个问题的问题。我的脚本正在尝试连接在消息上使用 wsse 并使用 HTTPS 配置的 Web 服务。我必须使用未经身份验证的代理。我使用 suds 使用服务如下:

import logging
import suds
import urllib2
from suds.client import Client
from suds.transport.https import HttpAuthenticated

#---These are not real values but represent them.---------
SERVICE_URL = 'https://service.com/1/soap'
SERVICE_USR = 'serviceusr'
SERVICE_PWD = 'servicepwd'
WSDL_URL = 'file:///folder/a.wsdl'
PROXY_NAME = 'proxy'
PROXY_PORT = 80
#----------------------------------------------------------

logging.basicConfig(level = logging.DEBUG)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)

proxy_info = {'host': PROXY_NAME,'port': PROXY_PORT}
proxyHandler = urllib2.ProxyHandler({'https':'https://(host)s:%(port)d/' % proxy_info})
opener = urllib2.build_opener(proxyHandler)
urllib2.install_opener(opener)
security = suds.wsse.Security()
token = suds.wsse.UsernameToken(SERVICE_USR, SERVICE_PWD)
security.tokens.append(token)
messageTransport = HttpAuthenticated(username = SERVICE_USR, password = SERVICE_PWD)
messageTransport.urlopner = opener
client = Client(url = WSDL_URL, location = SERVICE_URL, transport = messageTransport)
client.set_options(wsse=security)
result = client.service.ParseValidAddress('1 ABC Street Somwhere')

脚本运行后,我收到以下错误:

Error details: <class 'urllib2.URLError'> <urlopen error [Errno -2] Name or service not 
known>

所以我运行 curl 来检查所有网络位是否工作如下:

curl -x proxy:80 https://service1.com/1/soap

这将返回一个 SOAP 消息,指示网络配置和代理设置正确。那么为什么肥皂水不这样做呢?我哪里失败了?

4

2 回答 2

2

应该是

proxyHandler = urllib2.ProxyHandler({'https':'https://%(host)s:%(port)d/' % proxy_info})

(注意“https”和“%”末尾的“s”)。

于 2012-05-03T23:35:27.013 回答
0

成功!我做了以下,它的工作。在客户端添加proxySettings = {'https':'http://{0}:{1}'.format(PROXY_NAME, PROXY_PORT)}然后设置选项client.set_options(proxy = proxySettings)。感谢大家的帮助。

于 2012-05-04T00:15:49.573 回答