1

最近、Openstack の自動ログインを追加したいと考えています。そこで、openstack 用の python スパイダーを次のように記述します。

import urllib
import urllib2
import cookielib

cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

headers = {
    'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
}

postdata=urllib.urlencode({
    'username':'admin',
    'password':'1f22f83ac2724132',
    'region':'http://10.0.3.139:5000/v2.0',
    'csrfmiddlewaretoken':'xV5TyeQhItEhyMF7OvuI0PEAlMYmh9zM',
})

req = urllib2.Request(
    url = 'http://10.0.3.139/dashboard/auth/login/',
    data = postdata,
    headers = headers
)

result = opener.open(req)

print result.read()

しかし、結果は次のとおりです。

Traceback (most recent call last):
File "spider.py", line 25, in <module>
  result = opener.open(req)  
File "/usr/local/lib/python2.7/urllib2.py", line 410, in open
  response = meth(req, response)
File "/usr/local/lib/python2.7/urllib2.py", line 523, in http_response
  'http', request, response, code, msg, hdrs)
File "/usr/local/lib/python2.7/urllib2.py", line 448, in error
  return self._call_chain(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 382, in _call_chain
  result = func(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 531, in http_error_default
  raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: FORBIDDEN

すでに投稿データに「ヘッダー」を追加していますが、効果がないようです...次の方法がわかりません。誰かアドバイスをくれませんか??

4

1 に答える 1

1

以下のようなものが機能していました。

password = resp.resp
tenant_name = username
try:
    def safeConfigGet(sect,key):
        if config.has_option(sect,key):
            return config.get(sect,key)
        else:
            None
    connargs={
        'tenant': {
            'host': safeConfigGet('keystone','host'),
            'tenant': safeConfigGet('authby','tenant'),
        },
    }[identifier]
    auth_url = connargs['host']

    from keystoneclient.v2_0 import client
    from keystoneclient.v2_0 import tokens

    # keystone = client.Client(username=username, password=password, tenant_name=tenant_name, auth_url=auth_url)
    keystone = client.Client(username=username, password=password, auth_url=auth_url)
    token = keystone.auth_token
    headers = {'X-Auth-Token': token }
    tenant_url = auth_url
    tenant_url += '/tenants'
    r = requests.get(tenant_url, headers=headers)
    tenants_raw = r.raw.read(900000)
    tenant_data = json.loads(tenants_raw)
    success = 0
于 2013-10-29T14:53:57.933 に答える