0

誰でも助けてもらえますか?Python で SOAP リクエストを POST しようとしていますが、エラー Response 403: Forbidden が表示されます。私のコードは以下のようになります:私はPythonのインポートを使用しています:

import httplib
 import base64
 import string


    #the message
    message = """<soap:Envelope ...rest message </soap:Envelope>"""

    host = "host.test.com"

    url = 'https://server.etc.com' #End point url

基本認証も使用する必要があるため、http ヘッダーにユーザー名とパスワードが必要です

username = 'username'
    password = 'password'

   webSoapAction = 'urn:etc-com:document...'


        #the Authentication in base64 encoding form for Basic Authentication
        auth = 'Basic' + string.strip(base64.encodestring(username +'.'+ password))

        webservice = httplib.HTTP(host) #connect to the server(host) 

ここで私はヘッダーを構築しようとします:

webservice.putrequest("POST", url)
        webservice.putheader("Host", host)
        webservice.putheader("User-Agent", "Python http auth")


    webservice.putheader("Content-Type:", "text/xml; charset=\"UTF-8\"")
    webservice.putheader("Content-length", "%d" % len(message))
    webservice.putheader("SOAPAction",webSoapAction)

    webservice.putheader('Authorization', auth)

    webservice.endheaders()
    webservice.send(message)

私はここで応答を得る必要があります

#get the response
    statuscode, statusmessage, header = webservice.getreply()
    print "Response: ", statuscode, statusmessage
    print "Headers: ",header
    res = webservice.getfile().read()
    print 'Content: ', res
4

1 に答える 1

0

基本的な認証ヘッダーの構築に関して、次の 2 つの点があります。

  • 「Basic」とシークレットの間にスペースを 1 つ入れます
  • 「.」の代わりに「:」を使用します。ユーザー名とパスワードの間に

したがって、次のようになります。

#the Authentication in base64 encoding form for Basic Authentication
auth = 'Basic ' + string.strip(base64.encodestring(username +':'+ password))
于 2012-12-26T18:42:08.400 に答える