使用肥皂水的肥皂请求不起作用

时间:2017-10-13 08:33:08

标签: python web-services soap suds

我不得不向这个Web服务发出soap请求,但每次出现错误 请求是这样的:

POST /NewIPGServices/Sale/SaleService.asmx HTTP/1.1
Host: pec.shaparak.ir
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://pec.Shaparak.ir/NewIPGServices/Sale/SaleService/SalePaymentRequest"

<?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:Body>
    <SalePaymentRequest xmlns="https://pec.Shaparak.ir/NewIPGServices/Sale/SaleService">
      <requestData />
    </SalePaymentRequest>
  </soap:Body>
</soap:Envelope>

这是我的代码:

def ps_pymnt(request):
    location ="https://pec.Shaparak.ir/NewIPGServices/Sale/SaleService"
    PS_webService = "‫‪https://pec.shaparak.ir/NewIPGServices/Sale/SaleService.asmx‬‬"
    client=Client(PS_webService,location=location) #from suds.client import Client
    print(client)
    .
    .
    .
    return something

我在代码到达打印行之前遇到此错误:

Request Method: GET
Request URL: http://127.0.0.1:8002/payment/ps/pay/

Traceback:

File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
packages/django/core/handlers/base.py" in get_response
149.                     response = 
self.process_exception_by_middleware(e, request)

File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
packages/django/core/handlers/base.py" in get_response
147.                     response = wrapped_callback(request, 
*callback_args, **callback_kwargs)

File "/home/salman/salman/Projects/vira05/vira-285-
parsianPayment/virasciencecom/payment/views.py" in ps_port_payment
298.     client = Client(PS_webService, location=location)

 File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
 packages/suds/client.py" in __init__
 111.         self.wsdl = reader.open(url)

 File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
 packages/suds/reader.py" in open
 151.             d = self.fn(url, self.options)

 File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
 packages/suds/wsdl.py" in __init__
 135.         d = reader.open(url)

 File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
 packages/suds/reader.py" in open
  78.             d = self.download(url)

File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
packages/suds/reader.py" in download
  94.             fp = self.options.transport.open(Request(url))

File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
packages/suds/transport/http.py" in open
  174.         return HttpTransport.open(self, request)

File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
packages/suds/transport/http.py" in open
  63.             return self.u2open(u2request)

File "/home/salman/salman/Projects/vira04/virtual/lib/python3.5/site-
packages/suds/transport/http.py" in u2open
  119.             return url.open(u2request, timeout=tm)

File "/usr/lib/python3.5/urllib/request.py" in open
  466.         response = self._open(req, data)

 File "/usr/lib/python3.5/urllib/request.py" in _open
  489.                                 'unknown_open', req)

 File "/usr/lib/python3.5/urllib/request.py" in _call_chain
  444.             result = func(*args)

File "/usr/lib/python3.5/urllib/request.py" in unknown_open
   1324.         raise URLError('unknown url type: %s' % type)

Exception Type: URLError at /payment/ps/pay/
Exception Value: <urlopen error unknown url type: ‫‪https>

这是我第一次使用肥皂水,所以也许我犯了一个愚蠢的错误 我做错了什么?用python发送soap动作有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用requests。如果您还没有安装请求,可以使用

从pip工具安装它

pip install requests

发送GET请求

import requests

host = "‫‪https://pec.shaparak.ir/NewIPGServices/Sale/SaleService.asmx‬‬"
length = len(data)
headers = { 'Host'          : 'pec.shaparak.ir',
            'Content-Type'  : 'text/xml',
            'Encoding'      : 'utf-8',
            'Content-Length': length}

resp = requests.request("GET", host, data=data, headers=headers)

您可以使用resp.status_code获取请求响应HTTP代码,并使用resp.content获取完整响应。如果您需要原始数据,请将stream=True参数添加到请求中,并使用resp.raw.read(10)读取响应的10个字节,或使用resp.raw.data读取所有响应。