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 回答