您的位置:首页 > 编程语言 > Python开发

Python 访问soap服务

2017-05-17 17:31 211 查看
使用库:subs

soap服务信息:

网址:http://mobile.bjmemc.com.cn/AirService/Service.asmx

功能:使用其中的GetData服务获取北京各个地点的空气情况

GetData服务的请求示例:

POST /AirService/Service.asmx HTTP/1.1
Host: mobile.bjmemc.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetData"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<MySoapHeader xmlns="http://tempuri.org/">
<UserName>string</UserName>
<PassWord>string</PassWord>
</MySoapHeader>
</soap:Header>
<soap:Body>
<GetData xmlns="http://tempuri.org/">
<DevID>string</DevID>
<DevType>string</DevType>
</GetData>
</soap:Body>
</soap:Envelope>


Python代码的实现:

# -*- coding:UTF-8 -*-
from suds.client import Client

def getAirInfo():

url = 'http://mobile.bjmemc.com.cn/AirService/Service.asmx?wsdl'   #一般都是网址后面加个?wsdl,你可以打开看一下,是一堆xml文件
client = Client(url)   #创建一个客户端

header = client.factory.create('MySoapHeader')   #请求头,名字按他给的xml
header.UserName = '******'               #请求头里的用户名和密码,这是个付费服务
header.PassWord = '******'
client.set_options(soapheaders=[header,])      #将封装好的请求头加入到client中

result = client.service.GetData()           #调用GetData()服务,获取返回数据,返回一个text文本
print result
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐