python中的POST SOAP请求获取错误响应:403 Forbidden

时间:2012-12-24 15:31:11

标签: python-2.7

请有人帮忙吗?我试图在python中发布一个SOAP请求,但我收到错误响应403:禁止。我的代码如下所示: 我正在使用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

1 个答案:

答案 0 :(得分:0)

关于基本身份验证标头构造的两件事:

  • 在“基本”和您的秘密之间放置一个空格
  • 使用':'而不是'。'在用户名和密码之间

所以看起来应该是这样的:

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