2016-08-24 58 views
0

这是我尝试使用代码:使用套接字给出错误的免费SOAP服务的Python客户端?

import os 
import socket 
import httplib 

packet=''' 
<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
<soap12:Body> 
    <GetCitiesByCountry xmlns="http://www.webserviceX.NET"> 
    <CountryName>India</CountryName> 
    </GetCitiesByCountry> 
</soap12:Body> 
</soap12:Envelope>''' 
lent=len(packet) 
msg=""" 
POST "www.webservicex.net/globalweather.asmx" HTTP/1.1 
Host: www.webservicex.net 
Content-Type: application/soap+xml; charset=utf-8 
Content-Length: {length} 
SOAPAction:"http://www.webserviceX.NET/GetCitiesByCountry" 
Connection: Keep-Alive 
{xml}""".format(length=lent,xml=packet) 

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
client.connect(("www.webservicex.net",80)) 
if bytes != str: # Testing if is python2 or python3 
    msg = bytes(msg, 'utf-8') 
client.send(msg) 
client.settimeout(10) 
print(client.type) 
res=client.recv(4096) 
print(res) 
#res = res.replace("<","&lt;") -- only for stack overflow post 
#res = res.replace(">","&gt;") -- only for stack overflow post 

输出是:

HTTP/1.1 400 Bad Request                                   
Content-Type: text/html; charset=us-ascii                              
Server: Microsoft-HTTPAPI/2.0                                 
Date: Wed, 24 Aug 2016 11:40:02 GMT                                
Connection: close                                    
Content-Length: 324 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">;                 
<HTML><HEAD><TITLE>Bad Request</TITLE>                         
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>                  
<BODY><h2>Bad Request - Invalid URL</h2>                          
<hr><p>HTTP Error 400. The request URL is invalid.</p>                       
</BODY></HTML> 

任何想法?

回答

0

您应该使用suds来使用SOAP Web服务。

编辑:实例

import suds 
import suds.transport 
from suds.transport.http import HttpAuthenticated 


class MyException(Exception): 
    pass 


service_url = "http://my/service/url" 

获得可用的服务:

try: 
    transport = HttpAuthenticated(username='elmer', password='fudd') 
    wsdl = suds.client.Client(service_url, faults=False, transport=transport) 

except IOError as exc: 
    fmt = "Can initialize my service: {reason}" 
    raise MyException(fmt.format(reason=exc)) 

except suds.transport.TransportError as exc: 
    fmt = "HTTP error -- Is it a bad URL: {service_url}? {reason}" 
    raise MyException(fmt.format(service_url=service_url, raison=exc)) 

运行给定服务(在这里,它的名字叫了RunScript):

# add required options in headers 
http_headers = {'sessionID': 1} 
wsdl.set_options(soapheaders=http_headers) 

params = {"scriptLanguage": "javascript", "scriptFile": "...", 
      "scriptArgs": [{"name": "...", "value": "..."}]} 

try: 
    exit_code, response = wsdl.service.RunScript([params]) 
    # ... 
except suds.MethodNotFound as reason: 
    raise MyException("No method found: {reason}".format(reason=reason)) 
except suds.WebFault as reason: 
    raise MyException("Error running the script: {reason}".format(reason=reason)) 
except Exception as reason: 
    err_msg = "{0}".format(reason) 
    if err_msg == "timed out": 
     raise MyException("Timeout: {reason}".format(reason=reason)) 
    raise 

当然,这里不需要使用错误管理器。但我举了一个例子。与“超时”的最后一个是我用来检测我的应用程序超时的一种黑客。

+0

Thanks @Laurent,但是可以为任何SOAP Web服务起泡吗? –

+0

这是一个通用的SOAP客户端。我使用它在生产服务器上使用[InDesign CC服务器](http://www.adobe.com/fr/products/indesignserver.html)进行页面布局。它运作良好。 –

+0

非常感谢Laurent我能够使用这个:)调用函数这个按预期工作.. 您的先生的两个更多的问题.Thanx 1)在客户端网址,我认为它的强制性,该网址应喂养WSDL响应否则泡沫不会工作? url =“http://webservicex.net/globalweather.asmx” 抛出错误 2)任何关于Web服务的身份验证? 从suds.client进口客户 URL = “http://webservicex.net/globalweather.asmx?wsdl” 客户端=客户端(URL) 打印(客户端) 打印(client.service.GetCitiesByCountry( “印度” )) –

相关问题