1

我正在尝试使用 python/urllib2 向 Coverity Web 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 操作,它是可以访问的。有任何想法吗?

4

1 回答 1

1

该错误代码暗示它正在寻找基本或摘要身份验证。urllib2 有一个认证接口。PS 用户名和密码在 d 中未正确编码。如果其中有 < 之类的特殊字符,就会出现问题。

于 2016-04-28T14:20:58.337 回答