如何在Python中将Multipart/相关请求发送到SOAP服务器? [英] How to send Multipart/related requests in Python to SOAP server?

查看:100
本文介绍了如何在Python中将Multipart/相关请求发送到SOAP服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过多部分/相关的HTTP POST将文件发送到SOAP服务器.

I have to send a file to a SOAP server via a multipart/related HTTP POST.

我从头开始构建了这样的消息:

I have built the message from scratch like this:

from email.mime.application import MIMEApplication
from email.encoders import encode_7or8bit
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase

envelope = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xmime5="http://www.w3.org/2005/05/xmlmime" xmlns:osc-data="http://oracc.org/wsdl/ows.xsd" xmlns:osc-meth="http://oracc.org/wsdl/ows.wsdl"><SOAP-ENV:Body><osc-meth:Request><osc-data:keys><osc-data:key>atf</osc-data:key><osc-data:key>tests/mini</osc-data:key><osc-data:key>00atf/hyphens.atf</osc-data:key></osc-data:keys><osc-data:data><osc-data:item xmime5:contentType="*/*"><xop:Include href="cid:id6"/></osc-data:item></osc-data:data></osc-meth:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>"""

mtompkg = MIMEMultipart('related',boundary='============boundary============', charset='utf-8', type='application/xop+xml', start='<SOAP-ENV:Envelope>')
#Doesn't like the hyphen in start-info when passing as MIMEMultipart param
mtompkg.set_param('start-info', 'application/soap+xml')
mtompkg['Host'] = "http://oracc.museum.upenn.edu:8085"
mtompkg['Connection'] = "close"
del(mtompkg['mime-version'])

rootpkg = MIMEApplication(envelope, 'xop+xml', encode_7or8bit)
rootpkg.set_param('charset', 'utf-8')
rootpkg.set_param('type', 'application/soap+xml')
rootpkg.add_header('Content-ID', '<SOAP-ENV:Envelope>')
del(rootpkg['Content-Transfer-Encoding'])
rootpkg.add_header('Content-Transfer-Encoding', 'binary')
del(rootpkg['mime-version'])

mtompkg.attach(rootpkg)

document = MIMEBase('*','*')
document['Content-Transfer-Encoding'] = "binary"
document['Content-ID'] = "<id6>"
filename = "./request.zip"
document.set_payload(open(filename,'rb').read())
del(document['mime-version'])

mtompkg.attach(document)

bound = '--%s' % (mtompkg.get_boundary(), )
marray = mtompkg.as_string().split(bound)
mtombody = bound
mtombody += bound.join(marray[1:])

mtompkg.add_header("Content-Length", str(len(mtombody)))

这会导致一条消息与我从SOAP服务器开发人员那里获得的示例样本匹配,我们知道它可以正常工作:

Which results in a message matching the sample one I got from the SOAP server developer that we know works fine:

Content-Type: multipart/related; start="<SOAP-ENV:Envelope>"; charset="utf-8";
 type="application/xop+xml"; boundary="============boundary============";
 start-info="application/soap+xml"
Host: http://MY_URL:SOME_PORT
Connection: close
Content-Length: 1429

--============boundary============
Content-Type: application/xop+xml; charset="utf-8"; type="application/soap+xml"
Content-ID: <SOAP-ENV:Envelope>
Content-Transfer-Encoding: binary

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xmime5="http://www.w3.org/2005/05/xmlmime" xmlns:osc-data="http://oracc.org/wsdl/ows.xsd" xmlns:osc-meth="http://oracc.org/wsdl/ows.wsdl"><SOAP-ENV:Body><osc-meth:Request><osc-data:keys><osc-data:key>atf</osc-data:key><osc-data:key>tests/mini</osc-data:key><osc-data:key>00atf/hyphens.atf</osc-data:key></osc-data:keys><osc-data:data><osc-data:item xmime5:contentType="*/*"><xop:Include href="cid:id6"/></osc-data:item></osc-data:data></osc-meth:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>
--============boundary============
Content-Type: */*
Content-Transfer-Encoding: binary
Content-ID: <id6>

P�AtG.�Uen00atf/hyphens.atfUT   cOVJOVux
                                        �S
                                          04A.傢���+����b��L.�Ē4+���T�Ҽ���T.�PNb^�BEi���BuN飦�ڌDݤLݢR.��\+�Ĥ̢�h0��P�AtG.�Uen��00atf/hyphens.atfUTcOVux
                                                                    �PKW�
--============boundary============--

要发送它,我尝试了请求,urllib2和httplib,并且得到的行为是服务器永远挂起.我试图设置一个超时希望,希望它可以为我提供服务器的某种输出,但是它不起作用.这是我尝试过的:

To send it, I've tried requests, urllib2 and httplib and the behaviour I get is that the server hangs forever. I've tried setting up a timeout hoping that'd give me some kind of output from the server, but it doesn't work. This is what I've tried:

有请求:

import requests
url = 'http://MY_URL:SOME_PORT'
headers = dict(mtompkg.items())
body = mtompkg.as_string().split('\n\n', 1)[1]
response = requests.post(url, data=body, headers=headers)

使用urllib2:

import urllib2
request = urllib2.Request('http://MY_URL:SOME_PORT', body, headers)
urllib2.urlopen(request).read()

使用httplib:

import urlparse
import httplib
schema, netloc, url, params, query, fragments = urlparse.urlparse('http://MY_URL:SOME_PORT')
http = httplib.HTTPConnection(netloc)
http.connect()
http.putrequest("POST", 'http://MY_URL:SOME_PORT')
http.putheader('Content-type',  mtompkg['Host'])
http.putheader('Content-type',mtompkg['Content-Type'])
http.putheader('Content-Length", str(1429))
http.endheaders()
http.send(body)
response = http.getresponse()

suds,suds-jurko,SOAPpy等高级选项似乎不提供对多格式/相关附件的支持.我的测试用尽了,因此,非常感谢您提供任何帮助.

Higher level options like suds, suds-jurko, SOAPpy, etc. doesn't seem to offer support for multiform/related attachments. I am running out of options to test, so any kind of help would be most appreciated.

推荐答案

在此行之后添加以下行

body = mtompkg.as_string().split('\n\n', 1)[1]

#RFC spec asks you to use \r\n instead of python default of \n

body = body.replace('\n', '\r\n', 5)

这篇关于如何在Python中将Multipart/相关请求发送到SOAP服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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