如何使用urllib2向Coverity web api发送请求?

时间:2015-10-01 10:13:03

标签: python xml soap urllib2 coverity

我正在尝试使用python / urllib2向SOAP api发送SOAP xml请求(我无法安装新的python模块,因此我的选项仅限于urllib2)。

以下是我的代码:

def getDateTime():
   t = datetime.now()
   return t.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

def coverityRequest():
    dateAndTime = getDateTime()
    username = 'usernamestr'
    password = 'passstr'
    d = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v7="http://ws.coverity.com/v7"> \
    <soapenv:Header><wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:UsernameToken><wsse:Username>' + username +'</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' + password + '</wsse:Password><wsu:Created>' + dateAndTime + '</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header> \
   <soapenv:Body> \
      <v7:getSnapshotsForStream> \
         <streamId> \
            <name> abc.1.1_ab123 </name> \
         </streamId>  \
         <filterSpec> \
         </filterSpec> \
      </v7:getSnapshotsForStream> \
   </soapenv:Body> \
    </soapenv:Envelope>'

    r = urllib2.Request(url="http://x.x.x.x:8080/ws/v7/configurationservice?wsdl", data=d, headers={'Content-Type': 'text/xml'})
    u = urllib2.urlopen(r)
    response = u.read()
    return response

但是我得到以下输出:

Traceback (most recent call last):
  File "./gen_release_ticket_info.py", line 49, in <module>
    output = coverityRequest()
  File "./gen_release_ticket_info.py", line 44, in coverityRequest
    u = urllib2.urlopen(r)
  File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib64/python2.6/urllib2.py", line 397, in open
    response = meth(req, response)
  File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.6/urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 407: Proxy Authorization Required

我ping了服务器,它是可以访问的。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

该错误代码暗示它正在寻找基本或摘要式身份验证。 urllib2有一个身份验证接口。 PS用户名和密码未在d中正确编码。如果有像&lt;这样的特殊字符会有问题。在他们中间。