2014-02-06 137 views
1

我在嘗試使用Python中的SUDS訪問WS時出現此錯誤。TransportError:HTTP錯誤400:使用SUDS時出現錯誤請求(Python)

任何想法我做錯了什麼? (是的,我很新的Python)的

Python代碼

# -*- coding: utf-8 -*- 

from suds.client import Client 
from apscheduler.scheduler import Scheduler 
import datetime 
import logging 
from email.header import Header 
from email.mime.text import MIMEText 
from email.mime.application import MIMEApplication 
from email.mime.multipart import MIMEMultipart 
from smtplib   import SMTP_SSL 
from colorlog import ColoredFormatter 

# Configurações do Log 
logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S', filename='ConfirmarPedidoSmsBroker-Test.log',level=logging.INFO) 
logging.getLogger().addHandler(logging.StreamHandler()) 
now = datetime.datetime.now() 

#Informações para o consumo do WS 
WSDL_URL = "http://123.168.1.132:8084/abc-ws-provider/AbcBrokerBSv1.0?wsdl" 

#Criações dos tipos para gerar o objeto complexo 
class Evale(object): 
    codigoDeAutorizacao = 1234567 

class Cliente (object): 
    identificacaoDoCliente = 12345 
    codigoDoCliente = 101010 
    razaoSocial = "None" 

class Telefone (object): 
    ddd = 21 
    numero = 1234-5678 

class Contato (object): 
    telefone = Telefone() 

class HorariosDeAtendimento (object): 
    diaDaSemana = "Sunday" 
    horaInicial = "07:30:00" 
    horaFinal = "20:00:00" 

class TipoDeAtendimentoRealizado (object): 
    horariosDeAtendimento = HorariosDeAtendimento() 

class Revenda (object): 
    cliente = Cliente() 
    identificacaoDoEndereco = 987654 
    codigoDoEndereco = 8765567 
    numeroDoDocumento = "2312341" 
    filialDoDocumento = 0009 
    digitoDoDocumento = 32 

class RevendaPrincipal(object): 
    revenda = Revenda() 

class RevendaAlternativa(object): 
    revenda = Revenda() 

class EnderecoDoSolicitante(object): 
    logradouro = "Street Test" 
    numeroDoEndereco = 80 
    complemento = "Penth" 
    municipio = "New York" 
    bairro = "Brooklin" 
    cep = "74123" 
    estado = "NY" 

class PedidoSms(object): 
    identificacaoDaTransacao = 1010 
    evale = Evale() 
    revendaPrincipal = RevendaPrincipal() 
    revendaAlternativa = RevendaAlternativa() 
    enderecoDoSolicitante = EnderecoDoSolicitante() 
    latitudeDoEnderecoDoSolicitante = -92.123443 
    longitudeDoEnderecoDoSolicitante = -81.98765 

class ConfirmarPedidoSmsBrokerRequest(object): 
    pedidoSms = PedidoSms() 

class ConfirmarPedidoSmsBroker_InputVariable (object): 
    confirmarPedidoSmsBrokerRequest = ConfirmarPedidoSmsBrokerRequest() 

class Input (object): 
    confirmarPedidoSmsBroker_InputVariable = ConfirmarPedidoSmsBroker_InputVariable() 


# Método que chama o serviço 
def sendRequestToService(): 
    confPedido = Input() 
    logging.info("ConfirmarPedidoSmsBroker-Test at " + str(now)) 
    logging.info("WSDL_URL " + WSDL_URL) 
    logging.info("Passing URL to Client") 
    client = Client(WSDL_URL) 
    logging.info("Trying to access the service") 
    response = client.service.confirmarPedidoSmsBroker(confPedido) 
    logginf.info(response) 


if __name__ == '__main__': 
    sendRequestToService() 

這是我得到每當我嘗試運行它的錯誤。

06/02/2014 17:06:17 - ConfirmarPedidoSmsBroker-Test at 2014-02-06 17:06:17.443663 
06/02/2014 17:06:17 - WSDL_URL http://111.168.1.132:8084/abc-ws-provider/AbcBrokerBSv1.0?wsdl 
06/02/2014 17:06:17 - Passing URL to Client 
06/02/2014 17:06:17 - Import:0x10ab61d10, imported schema (http://www.abcdef.com.br/Abab/BO/v3) at (http://123.168.1.132:8084/../../../../../Abc/Def/Somethingv3.xsd), failed 
Traceback (most recent call last): 
    File "build/bdist.macosx-10.9-intel/egg/suds/xsd/sxbasic.py", line 560, in download 
    d = reader.open(url) 
    File "build/bdist.macosx-10.9-intel/egg/suds/reader.py", line 79, in open 
    d = self.download(url) 
    File "build/bdist.macosx-10.9-intel/egg/suds/reader.py", line 95, in download 
    fp = self.options.transport.open(Request(url)) 
    File "build/bdist.macosx-10.9-intel/egg/suds/transport/https.py", line 60, in open 
    return HttpTransport.open(self, request) 
    File "build/bdist.macosx-10.9-intel/egg/suds/transport/http.py", line 64, in open 
    raise TransportError(str(e), e.code, e.fp) 
TransportError: HTTP Error 400: Bad Request 

回答

0

看來你的WSDL服務與400錯誤響應爲你傳遞的請求(也許它缺少必需的參數,等等。)請儘量add debug logging像這樣:

import logging 
logging.basicConfig(level=logging.DEBUG) 
logging.getLogger('suds.client').setLevel(logging.DEBUG) 

後(可以打開交互式python shell並粘貼代碼,啓用日誌並調用sendRequestToService函數。

請修改您的問題並更新,我會修改m y適當地回答。