首页 > 解决方案 > 如何使用 python 与 Web 服务建立经过身份验证的连接?

问题描述

我正在尝试验证与 Python 上的 soap API 的连接。程序很简单,如果 authenticated 为 true,printing"hello world"或 authenticated 是错误的,printing "password or username is wrong" I try to authenticate with python,不幸的是,控制台上什么也没有出现。进行身份验证连接后,如何在屏幕上获取要打印的目标字符串的文本?

Python代码:

from requests.auth import HTTPBasicAuth  # or HTTPDigestAuth, or OAuth1, etc.
from requests import Session
from zeep import Client
from zeep.transports import Transport
wsdl='http://localhost:49280/orhan.asmx?WSDL'
user = 'test'
password = 'test'

session = Session()
session.auth = HTTPBasicAuth(user, password)
client = Client(wsdl,
            transport=Transport(session=session))

http://localhost:49280/orhan.asmx?WSDL是 :

<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/">
<link type="text/css" rel="stylesheet" id="dark-mode-general-link"/>
<link type="text/css" rel="stylesheet" id="dark-mode-custom-link"/>
<style type="text/css" id="dark-mode-custom-style"/>
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="ad" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="AuthHeader" type="tns:AuthHeader"/>
<s:complexType name="AuthHeader">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string"/>
</s:sequence>
<s:anyAttribute/>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:message name="HelloWorldSoapIn">
<wsdl:part name="parameters" element="tns:HelloWorld"/>
</wsdl:message>
<wsdl:message name="HelloWorldSoapOut">
<wsdl:part name="parameters" element="tns:HelloWorldResponse"/>
</wsdl:message>
<wsdl:message name="HelloWorldAuthHeader">
<wsdl:part name="AuthHeader" element="tns:AuthHeader"/>
</wsdl:message>
<wsdl:portType name="orhanSoap">
<wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn"/>
<wsdl:output message="tns:HelloWorldSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="orhanSoap" type="tns:orhanSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://tempuri.org/HelloWorld" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
<soap:header message="tns:HelloWorldAuthHeader" part="AuthHeader" use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="orhanSoap12" type="tns:orhanSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="HelloWorld">
<soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
<soap12:header message="tns:HelloWorldAuthHeader" part="AuthHeader" use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="orhan">
<wsdl:port name="orhanSoap" binding="tns:orhanSoap">
<soap:address location="http://localhost:49280/orhan.asmx"/>
</wsdl:port>
<wsdl:port name="orhanSoap12" binding="tns:orhanSoap12">
<soap12:address location="http://localhost:49280/orhan.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

标签: pythonsoapwsdlasmx

解决方案


推荐阅读