2

次のpythoncurl呼び出しは、次の成功した結果をもたらします。

>>> 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とurllib2は、SSLがソケットにコンパイルされている場合にのみ機能する可能性があることを理解しています。SSLはソケットのモジュールにコンパイルされています。実際、私は別のAPIでの他の呼び出しにurllib2を正常に使用しました。問題は、この1つの特定のAPIに限定されています。

urllib2(およびhttplib)はcurlとは何が違うのですか?

注:使用されるユーザー名とパスワードは、すべての例で同じです。

アップデート:

問題は、基本認証パスワードマネージャーにあります。基本認証ヘッダーを手動で追加すると、urllib2calは機能します。

>>> 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>'
4

2 に答える 2

3

Pythonurllib2から基本認証の問題

問題は、HTTP-Standardに従って、Pythonライブラリが最初に認証されていないリクエストを送信し、次に401再試行で応答された場合にのみ、正しいクレデンシャルが送信されることです。...サーバーが「完全に標準的な認証」を行わない場合、ライブラリは機能しません。

この特定のAPIは、最初の試行で401 Unauthorizedで応答せず、クレデンシャルが200OK応答コードで送信されなかったというメッセージを含むXML応答で応答します。

于 2012-03-02T17:19:07.803 に答える
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()
于 2012-02-29T07:47:27.403 に答える