简体   繁体   English

Python:用suds发出请求

[英]Python: Making a request with suds

i'm testing out SUDS library and I'm trying to make a simple request to an endpoint but i get unusual output. 我正在测试SUDS库,我正在尝试向端点发出一个简单的请求,但我得到了不寻常的输出。 Why? 为什么?

from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)

url = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway"

client = Client(url)
print client

Output: 输出:

Martynass-MacBook-Air:CH martynas$ python ch.py
DEBUG:suds.xsd.schema:loaded:

schema collection
   Schema:0x109a7db90
   (raw)
      <schema/>
   (model)

DEBUG:suds.xsd.schema:MERGED:
Schema:0x109a7db90
(raw)
   <schema/>
(model)

You can't use suds for this servce, suds is based on SOAP, which is another web service protocol. 你不能为这个服务使用suds,suds基于SOAP,这是另一个web服务协议。 What you can do is send an xml request and get a response. 您可以做的是发送xml请求并获得响应。

import requests

target_url = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway"
headers={'Content-type': 'text/xml'}
print requests.post(target_url, data=xml, headers=headers).text

Where the xml is defined according to their schemes. 其中xml是根据他们的方案定义的。 http://xmlgw.companieshouse.gov.uk/example_http.html This is one examaple http://xmlgw.companieshouse.gov.uk/example_http.html这是一个例子

xml = ('''
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader" 
xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 
xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader">
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>CompanyDetails</Class>
<Qualifier>request</Qualifier>
<TransactionID>14456553</TransactionID>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>My_SenderID</SenderID>
<Authentication>
<Method>CHMD5</Method>
<Value>e999e113407884fa410fa2f53bc23952</Value>
</Authentication>
</IDAuthentication>
<EmailAddress>sometest@some.email.address</EmailAddress>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys/>
</GovTalkDetails>
<Body>
<CompanyDetailsRequest xmlns="http://xmlgw.companieshouse.gov.uk/v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/CoDets.xsd">
<CompanyNumber>01002361</CompanyNumber>
<GiveMortTotals>1</GiveMortTotals>
</CompanyDetailsRequest>
</Body>
</GovTalkMessage>
''')

<Class>CompanyDetails</Class> What type of info you're getting. <Class>CompanyDetails</Class>您获得的信息类型。 kinda what "function" to call 有点什么“功能”来打电话

<Authentication>
<Method>CHMD5</Method>
<Value>e999e113407884fa410fa2f53bc23952</Value>
</Authentication>
</IDAuthentication>
<Authentication>
<Method>CHMD5</Method>
<Value>e999e113407884fa410fa2f53bc23952</Value>
</Authentication>
</IDAuthentication>
Here you would put the login info i guess
<Authentication>
<Method>CHMD5</Method>
<Value>e999e113407884fa410fa2f53bc23952</Value>
</Authentication>
</IDAuthentication>
我想在这里输入登录信息

<CompanyDetailsRequest xmlns="http://xmlgw.companieshouse.gov.uk/v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/CoDets.xsd"> <CompanyNumber>01002361</CompanyNumber> <GiveMortTotals>1</GiveMortTotals> </CompanyDetailsRequest> The "function" call and it's parameters <CompanyDetailsRequest xmlns="http://xmlgw.companieshouse.gov.uk/v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/CoDets.xsd"> <CompanyNumber>01002361</CompanyNumber> <GiveMortTotals>1</GiveMortTotals> </CompanyDetailsRequest> ”函数“调用及其参数

Now this will give me a response telling me the authorization failed. 现在,这将给我一个回复,告诉我授权失败。 So if you have an account there, this should work for you. 因此,如果您有帐户,这应该适合您。

Here you can find the list of schemes they have for different types of request. 在这里,您可以找到他们针对不同类型请求的方案列表。 Some of them have sample request to help you out. 他们中的一些人有样品请求帮助你。 http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/SchemaStatusOutput http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/SchemaStatusOutput

Here is the complete guide of all their schemes. 这是他们所有计划的完整指南。 http://xmlgw.companieshouse.gov.uk/data_usage_guide_dec_2013.pdf http://xmlgw.companieshouse.gov.uk/data_usage_guide_dec_2013.pdf

There aren't any wsdl definitions for that site. 该站点没有任何wsdl定义。 Try something like http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL for your url then you can try something like client.service.GetWeatherInformation() 尝试使用http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL等网址,然后尝试使用client.service.GetWeatherInformation()

From the suds document, "You will need to know the url for WSDL for each service used." 从suds文档中,“您将需要知道所使用的每个服务的WSDL URL。”

An explicit example 一个明确的例子

from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)

url = " http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"

client = Client(url)
client.service.GetWeatherInformation()

Outputs a ton of data. 输出大量数据。

Suds does not make it easy to discover the service, it's better to first test a bit with soapui or generate a human-readable doc of the wsdl with this xslt : http://code.google.com/p/wsdl-viewer/ . Suds并不容易发现这项服务,最好先用soapui测试一下,或者用xslt生成一个人类可读的wsdl文档: http//code.google.com/p/wsdl-viewer/ So you know the structure of requests and replies, and which services are available. 因此,您了解请求和回复的结构以及可用的服务。

Requests and responses in soap are xml trees, so once you get the result, you need to access the content of the xml tag that contains the information you're interested in. Here is an example that should work ( I don't have a username, but the result.Status.Success works ). soap中的请求和响应是xml树,所以一旦得到结果,你需要访问包含你感兴趣的信息的xml标签的内容。这是一个应该有效的例子(我没有用户名,但结果.Status.Success工作)。

import suds

client = suds.client.Client("http://webservices.data-8.co.uk/companieshouse.asmx?WSDL")
result = client.service.GetCompanyDetails("username", "password", 1234)
print result.Status.Success
print result.Result.CompanyName

You can not make a request against .xsd. 您无法针对.xsd提出请求。 XSD is definition of the exchanged message. XSD是交换消息的定义。 You must make a request against webservice Looking here you can find more info about that web service. 您必须针对Web服务提出请求。 在此处,您可以找到有关该Web服务的更多信息。 But also there is pricing page indicating that you must pay to use their service. 但也有定价页面表明您必须付费才能使用他们的服务。 Probably when you pay you will get username and password to authenticate with the service. 可能在您付款时,您将获得用户名和密码以对服务进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM