Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 python中的POST SOAP请求获取错误响应:403禁止_Python 2.7 - Fatal编程技术网

Python 2.7 python中的POST SOAP请求获取错误响应:403禁止

Python 2.7 python中的POST SOAP请求获取错误响应:403禁止,python-2.7,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 = 'h

请问有人能帮忙吗?我试图在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
在这里,我尝试构建标题:

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

关于基本的auth头构造,有两件事:

  • 在“基本”和“秘密”之间留出一个空格
  • 在用户名和密码之间使用“:”而不是“.”
所以看起来应该是:

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

谢谢你,雷克斯。我不知道这是否与使用http(主机)或httpconnection(主机)有关。webservice=httplib.HTTP(主机)webservice=httplib.HTTPConnection(主机,80)
#the Authentication in base64 encoding form for Basic Authentication
auth = 'Basic ' + string.strip(base64.encodestring(username +':'+ password))