python调用https的webservice接口

python调用https的webservice接口

对接了一个项目,啥年代了,接口居然是soup风格的接口,返回的参数还不是json。最最坑的是这种接口https调用是最不方便的,普遍都是调用http接口。但项目明确需要运行https。和接口的研发沟通了一个月才找到他们的https调用方法。

先分享我尝试的失败方法。
首先是我的原本代码,很显然调用失败。

# 原本的代码
from suds.client import Client
from suds.transport.https import HttpAuthenticated

url = 'https://x.x.x.x/x/x/x.wsdl'
client = Client(url_policy,username="x",password="x")

api_function_args = {"参数": "参数"}
tmp = dict(client.service.xxx({"参数": "参数"}))
print(tmp)

于是我收集了网上https调用wsdl的各种方法

# 以使用具有特定HTTPSHander的ssl context
from suds.client import Client
from suds.transport.https import HttpAuthenticated
import ssl
from urllib.request import HTTPSHandler
class CustomTransport(HttpAuthenticated):
    def u2handlers(self):
        handlers = HttpAuthenticated.u2handlers(self)
        ctx = ssl._create_unverified_context()
        ctx.check_hostname = False
        handlers.append(HTTPSHandler(context=ctx))
        return handlers

url = 'https://x.x.x.x/x/x/x.wsdl'
client = Client(url,username="x",password="x")

api_function_args = {"参数": "参数"}
tmp = dict(client.service.xxx(api_function_args))
print(tmp)
# ssl context
from suds.client import Client
from suds.transport.https import HttpAuthenticated
import ssl

if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context

url = 'https://x.x.x.x/x/x/x.wsdl'
client = Client(url,username="x",password="x")

api_function_args = {"参数": "参数"}
tmp = dict(client.service.xxx(api_function_args))
print(tmp)
import ssl
from unittest import mock
from suds.client import Client
from suds.transport.https import HttpAuthenticated

url = 'https://x.x.x.x/x/x/x.wsdl'
client = Client(url,username="x",password="x")

api_function_args = {"参数": "参数"}
tmp = dict(client.service.xxx(api_function_args))
print(tmp)

成功的调用方法

在双方沟通之后才发现,需要以下方式去调用https。他们的https调用接口需要更换特定的接口。
这我怎么知道。

from suds.client import Client
from suds.transport.https import HttpAuthenticated
import ssl

# 取消verify
ssl._create_default_https_context = ssl._create_unverified_context

# 进行基础认证
authentication = HttpAuthenticated(username="xxx", password="xxx") 

ip = "x.x.x.x"
# 需要使用的webservice接口地址
rest = "/x/x/x.wsdl"
# 调用的https接口
https_rest = "/x/x/x"

url = "https://" + ip + rest
url_lo = "https://" + ip + https_rest

client = Client(url, transport=authentication)

client.set_options(location=url_lo)

tmp = dict(client.service.xxx({"参数": "参数"}))
print(tmp)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值