python 向一个接口地址发送一段xml请求,然后收到一个返回

但丁_ 2011-07-08 03:18:39
具体是这样的,我有一个接口地址,根据需求需要向这个接口地址发送一段xml 数据。然后会收到一个响应,再根据响应内容,做相应处理。
网上找了一段代码,应该是我需要的,但是里面有些部分我不是很明白,在此请教各位了,代码如下,请知道的大大们帮忙讲一下。

import urllib2
import sys, httplib
def SendRtx(target,title,content):
SENDTPL = \
'''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.oa.com/common/message">
<SOAP-ENV:Body>
<ns1:SendRTX>
<ns1:sender>dantezhu</ns1:sender>
<ns1:receiver>%s</ns1:receiver>
<ns1:title>%s</ns1:title>
<ns1:msgInfo>%s</ns1:msgInfo>
<ns1:messageType>0</ns1:messageType>
</ns1:SendRTX>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>'''
SoapMessage = SENDTPL % (target,title,content)
webservice = httplib.HTTP("ws.oa.com")
webservice.putrequest("POST", "/messageservice.asmx")
webservice.putheader("Host", "ws.oa.com")
webservice.putheader("User-Agent", "Python Post")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(SoapMessage))
webservice.putheader("SOAPAction", "\"http://ws.oa.com/common/message/SendRTX\"")
webservice.endheaders()
webservice.send(SoapMessage)
# get the response
statuscode, statusmessage, header = webservice.getreply()
...全文
1083 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
seuer126 2011-07-18
  • 打赏
  • 举报
回复

post 提交请求 get 获得回应 ,这个应该不难吧
anqingxuexi 2011-07-11
  • 打赏
  • 举报
回复
这个很简单吧,就是http报头,发一个http请求,然后再get恢复啊,我觉得你最好看下http协议
Csuxiaowu 2011-07-10
  • 打赏
  • 举报
回复
luguo
panghuhu250 2011-07-09
  • 打赏
  • 举报
回复
具体是哪儿不明白?

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧