gpt4 book ai didi

用于 Openstack 登录的 Python 蜘蛛

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:21 27 4
gpt4 key购买 nike

最近想为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

我已经在发布数据中添加了“标题”。但它似乎不起作用......我不知道下一步该怎么做。有人可以告诉我一些建议吗?

最佳答案

像下面这样的东西曾经有效。

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

关于用于 Openstack 登录的 Python 蜘蛛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19651369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com