我正在尝试使用SOAPpy作为客户端发送SOAP请求。我找到了一些说明如何通过扩展SOAPpy.HTTPTransport添加cookie的文档,但我似乎无法使其正常工作。
我尝试使用示例here,但我试图向其发送请求的服务器开始抛出415错误,所以我试图在不使用ClientCookie的情况下完成此操作,或者通过弄清楚为什么我使用它时服务器会抛出415错误。我怀疑这可能是因为ClientCookie使用了urllib2 & http/1.1,而SOAPpy使用了urllib & http/1.0
有没有人知道如何让ClientCookie使用http/1.0,或者知道如何在不使用ClientCookie的情况下将cookie添加到SOAPpy头文件中?如果使用其他服务尝试此代码,则似乎只有在向Microsoft服务器发送请求时才会抛出错误。
我仍然在寻找python的立足点,所以可能只是我在做一些愚蠢的事情。
import sys, os, string
from SOAPpy import WSDL,HTTPTransport,Config,SOAPAddress,Types
import ClientCookie
Config.cookieJar = ClientCookie.MozillaCookieJar()
class CookieTransport(HTTPTransport):
def call(self, addr, data, namespace, soapaction = None, encoding = None,
http_proxy = None, config = Config):
if not isinstance(addr, SOAPAddress):
addr = SOAPAddress(addr, config)
cookie_cutter = ClientCookie.HTTPCookieProcessor(config.cookieJar)
hh = ClientCookie.HTTPHandler()
hh.set_http_debuglevel(1)
# TODO proxy support
opener = ClientCookie.build_opener(cookie_cutter, hh)
t = 'text/xml';
if encoding != None:
t += '; charset="%s"' % encoding
opener.addheaders = [("Content-Type", t),
("Cookie", "Username=foobar"), # ClientCookie should handle
("SOAPAction" , "%s" % (soapaction))]
response = opener.open(addr.proto + "://" + addr.host + addr.path, data)
data = response.read()
# get the new namespace
if namespace is None:
new_ns = None
else:
new_ns = self.getNS(namespace, data)
print '\n' * 4 , '-'*50
# return response payload
return data, new_ns
url = 'http://www.authorstream.com/Services/Test.asmx?WSDL'
proxy = WSDL.Proxy(url, transport=CookieTransport)
print proxy.GetList()发布于 2009-06-30 04:56:47
错误415是因为内容类型标头不正确。
安装httpfox for firefox或任何工具(wireshark、Charles或Fiddler)来跟踪您正在发送的报头。尝试Content-Type: application/xml。
...
t = 'application/xml';
if encoding != None:
t += '; charset="%s"' % encoding
...如果您尝试将文件发送到web服务器,请使用Content-Type:application/x-www-form-urlencoded
发布于 2013-05-09 19:58:44
在SOAPpy中使用cookie的一个很好的技巧是调用Using Cookies with SOAPpy calls
https://stackoverflow.com/questions/1061690
复制相似问题