如何使用python与web服务建立经过身份验证的连接?

如何使用python与web服务建立经过身份验证的连接?,python,soap,wsdl,asmx,Python,Soap,Wsdl,Asmx,我正在尝试使用Python上的soap API对连接进行身份验证。 程序很简单,如果验证为真,则打印“hello world”,或者验证为错误,则打印“密码或用户名错误” 我尝试使用python进行身份验证,不幸的是,控制台上没有显示任何内容。如何在建立“已验证到”连接后,在屏幕上获得要打印的文本目标字符串 Python代码: from requests.auth import HTTPBasicAuth # or HTTPDigestAuth, or OAuth1, etc. from re

我正在尝试使用Python上的soap API对连接进行身份验证。 程序很简单,如果验证为真,则打印
“hello world”
,或者验证为错误,则打印
“密码或用户名错误”
我尝试使用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>


你在混合东西。基本身份验证在Web服务器上完成,您指定的用户名和密码是调用的一部分,但不是soap消息的一部分。如果用户名/pw组合失败,您将从Web服务器获得http 40x错误。如果他们是正确的,您的请求将提供给Web服务。然后,如果您指定了正确的输入消息,Web服务可以进行回复。从那里你可以得到一个soap响应或一个soap错误。你在混淆视听。基本身份验证在Web服务器上完成,您指定的用户名和密码是调用的一部分,但不是soap消息的一部分。如果用户名/pw组合失败,您将从Web服务器获得http 40x错误。如果他们是正确的,您的请求将提供给Web服务。然后,如果您指定了正确的输入消息,Web服务可以进行回复。从那里可以得到soap响应或soap错误。