最近,我想为 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
我已经在帖子数据中添加了“标题”。但它似乎无效......我不知道下一步该怎么做。有人可以告诉我一些建议吗?