1

Recently, I want to add auto login for Openstack. So I write a python spider for openstack like this:

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()

But the result is :

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

I has already add "header" in post data.But it seems to be uneffective... I don not know how to do next. Could someone tell me some advice ??

changzhi
  • 2,641
  • 9
  • 36
  • 46
  • have a look at this, maybe it can help: http://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden/13303773#13303773 – andrean Oct 29 '13 at 07:04

1 Answers1

1

Something like below used to work.

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
Matt Joyce
  • 2,010
  • 2
  • 20
  • 31
  • Could you tell me the detailed code ?? I dont not understand the above code..Thank you!! – changzhi Oct 30 '13 at 01:21
  • basically i am using the python keystone client methods to authenticate to the keystone API. the client.Client() method is imported from keystoneclient.v2_0 as client. Then I store my token to variable token. pretty straight forward. I also do a requests query to the keystone API to get a list of my own tenant membership. This allows me to then further grab tokens for all my tenants. – Matt Joyce Oct 30 '13 at 01:27
  • Yes, I understand what you say. I want to realize a function whcih can auto login openstack by registered user ,such as send a POST or GET method I guess.. – changzhi Oct 30 '13 at 01:31
  • err sorry auth can be a get... the token has to be as a header X-Auth-Token. – Matt Joyce Oct 30 '13 at 01:55
  • I think that I can send a POST method like this: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' , 'X-Auth-Token': token, } postdata=urllib.urlencode({ 'username':'admin', 'password':'1f22f83ac2724132', 'region':'http://10.0.3.139:5000/v2.0', }) I can login automatically. Does the thought right ? – changzhi Oct 30 '13 at 03:17