简体   繁体   English

如何使用urllib2向Coverity Web API发送请求?

[英]How to send a request to Coverity web api using urllib2?

I'm trying to send a SOAP xml request to coverity web api using python/urllib2 (I'm unable to install new python modules, so my options are limited to urllib2). 我正在尝试使用python / urllib2将SOAP xml请求发送到Coverity Web api(我无法安装新的python模块,因此我的选择仅限于urllib2)。

Below is my code: 下面是我的代码:

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

However I'm getting the below output: 但是我得到以下输出:

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

I ping-ed the server and it is reachable. 我对服务器执行ping操作,并且可以访问。 Any ideas? 有任何想法吗?

That error code hints that it's looking for Basic or Digest authentication. 该错误代码表明它正在寻找基本或摘要式身份验证。 urllib2 has an authentication interface. urllib2具有身份验证接口。 PS The username and password aren't encoded properly in d. PS用户名和密码在d中没有正确编码。 There will be problems if there are special characters like < in them. 如果其中包含特殊字符(例如<),将会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM