以下python curl调用具有以下成功结果:
>>> import subprocess
>>> args = [
'curl',
'-H', 'X-Requested-With: Demo',
'https://username:password@qualysapi.qualys.com/qps/rest/3.0/count/was/webapp' ]
>>> xml_output = subprocess.check_output(args).decode('utf-8')
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
138 276 0 276 0 0 190 0 --:--:-- 0:00:01 --:--:-- 315
>>> xml_output
u'<?xml version="1.0" encoding="UTF-8"?>\n<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://qualysapi.qualys.com/qps/xsd/3.0/was/webapp.xsd">\n<responseCode>SUCCESS</responseCode>\n <count>33</count>\n</ServiceResponse>'
不幸的是,此调用未成功转换为urllib2。我收到一个不同的XML响应,声明用户没有提供授权凭据:
>>> import urllib2
>>> # Create a password manager.
... password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
>>> # Add the username and password.
... top_level_url = 'https://qualysapi.qualys.com'
>>> password_mgr.add_password(None, top_level_url, username, password)
>>> handler = urllib2.HTTPBasicAuthHandler(password_mgr)
>>> opener = urllib2.build_opener(handler)
>>> urllib2.install_opener(opener)
>>> headers = {'X-Requested-With':'Demo'}
>>> uri = 'https://qualysapi.qualys.com/qps/rest/3.0/count/was/webapp'
>>> req = urllib2.Request(uri,None,headers)
>>> result = urllib2.urlopen(req)
>>> result
'<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://qualysapi.qualys.com/qps/xsd/3.0/was/webapp.xsd">\n <responseCode>INVALID_CREDENTIALS</responseCode>\n <responseErrorDetails>\n <errorMessage>User did not supply any authentication headers</errorMessage>\n </responseErrorDetails>\n</ServiceResponse>'
顺便说一下,我收到了与httplib相同的错误消息:
>>> import httplib, base64
>>> auth = 'Basic ' + string.strip(base64.encodestring(username + ':' + password))
>>> h = httplib.HTTPSConnection('qualysapi.qualys.com')
>>> h.request("GET", "/qps/rest/3.0/count/was/webapp/")
>>> r1 = h.getresponse()
>>> print r1.status, r1.reason
200 OK
>>> data1 = r1.read()
>>> data1
'<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://qualysapi.qualys.com/qps/xsd/3.0/was/webapp.xsd">\n <responseCode>INVALID_CREDENTIALS</responseCode>\n <responseErrorDetails>\n <errorMessage>User did not supply any authentication headers</errorMessage>\n </responseErrorDetails>\n</ServiceResponse>'
我理解httplib&amp; urllib2可能仅在SSL被编译到套接字时才能工作,而SSL被编译到套接字模块中。事实上,我已成功使用urllib2进行其他API上的其他调用。问题与这个特定的API隔离开来。
什么是urllib2(和httplib)与curl的不同之处?
注意:所有示例中使用的用户名和密码都相同。
更新
问题在于基本的auth密码管理器。当我手动添加基本授权标头时,urllib2 cal工作:
>>> import base64
>>> base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
>>> req.add_header("Authorization", "Basic %s" % base64string)
>>> # Make request to fetch url.
... result = urllib2.urlopen(req)
>>> # Read xml results.
... xml = result.read()
>>> xml
'<?xml version="1.0" encoding="UTF-8"?>\n<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://qualysapi.qualys.com/qps/xsd/3.0/was/webapp.xsd">\n <responseCode>SUCCESS</responseCode>\n <count>33</count>\n</ServiceResponse>'
答案 0 :(得分:3)
来自Python urllib2 Basic Auth Problem
问题是,根据HTTP-Standard,Python库首先发送未经身份验证的请求,然后只有在回复401重试时,才会发送正确的凭据。如果...服务器不进行“完全标准认证”,则库将无法工作。
此特定API在第一次尝试时未响应401 Unauthorized,它以XML响应作为响应,其中包含未使用200 OK响应代码发送凭据的消息。
答案 1 :(得分:0)
尝试设置用户代理,也许这就是干扰。 urllib2将自己标识为Python-urllib/x.y
(其中x和y是Python版本的主要和次要版本号,例如Python-urllib/2.5
)这可能是导致网站阻止您的请求的原因。看看他们的robots.txt ..这里是一个关于设置用户代理的示例,以便您将脚本标识为浏览器:
import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()