Python3 error `IndexError: list index out of range`
Python3 error `IndexError: list index out of range`
大家好,我是 python 的新手,正在尝试调试下面的错误。该脚本基本上在我的 Gitlab 端点上创建了一个 PAT(个人访问令牌)。
Traceback (most recent call last):
File "/Test/test-gitlabtoken.py", line 86, in <module>
main()
File "/Test/test-gitlabtoken.py", line 79, in main
name = sys.argv[1]
IndexError: list index out of range
下面是我从这里提取的完整脚本 --> https://github.com/vitalyisaev2/gitlab_token/blob/main/personal_access_token.py
#!/usr/bin/python3
"""
Script that creates Personal Access Token for Gitlab API;
Tested with:
- Gitlab Community Edition 10.1.4
- Gitlab Enterprise Edition 12.6.2
- Gitlab Enterprise Edition 13.4.4
"""
import sys
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
endpoint = "https://blah.com" # updated
root_route = urljoin(endpoint, "/")
sign_in_route = urljoin(endpoint, "/users/sign_in")
pat_route = urljoin(endpoint, "/-/profile/personal_access_tokens")
login = "root"
password = "pass" # updated
def find_csrf_token(text):
soup = BeautifulSoup(text, "lxml")
token = soup.find(attrs={"name": "csrf-token"})
param = soup.find(attrs={"name": "csrf-param"})
data = {param.get("content"): token.get("content")}
return data
def obtain_csrf_token():
r = requests.get(root_route)
token = find_csrf_token(r.text)
return token, r.cookies
def obtain_authenticity_token(cookies):
r = requests.get(pat_route, cookies=cookies)
soup = BeautifulSoup(r.text, "lxml")
token = soup.find('input', attrs={'name': 'authenticity_token', 'type': 'hidden'}).get('value')
return token
def sign_in(csrf, cookies):
data = {
"user[login]": login,
"user[password]": password,
"user[remember_me]": 0,
"utf8": "✓"
}
data.update(csrf)
r = requests.post(sign_in_route, data=data, cookies=cookies)
token = find_csrf_token(r.text)
return token, r.history[0].cookies
def obtain_personal_access_token(name, expires_at, csrf, cookies, authenticity_token):
data = {
"personal_access_token[expires_at]": expires_at,
"personal_access_token[name]": name,
"personal_access_token[scopes][]": "api",
"authenticity_token": authenticity_token,
"utf8": "✓"
}
data.update(csrf)
r = requests.post(pat_route, data=data, cookies=cookies)
soup = BeautifulSoup(r.text, "lxml")
token = soup.find('input', id='created-personal-access-token').get('value')
return token
def main():
csrf1, cookies1 = obtain_csrf_token()
print("root", csrf1, cookies1)
csrf2, cookies2 = sign_in(csrf1, cookies1)
print("sign_in", csrf2, cookies2)
authenticity_token = obtain_authenticity_token(cookies2)
name = sys.argv[1]
expires_at = sys.argv[2]
token = obtain_personal_access_token(name, expires_at, csrf2, cookies2, authenticity_token)
print(token)
if __name__ == "__main__":
main()
我的 python 版本是 Python 3.9.7
我将不胜感激任何指示。谢谢
搜索错误(源)行时:
name = sys.argv[1]
你可以找到这个:
Meaning sys argv
这里的问题是 sys.argv 给出的列表没有提供第二个属性。
如果您使用 Spyder IDE 编码,您可能会在 link 中找到一些调试帮助:
否则您可能会尝试使用 Python 调试器 (Pdb)。
感谢您的反馈。我在命令行中遗漏了一些参数。所以我可以使用下面的命令解锁自己:
python3 personal_access_token.py mytoken 2022-08-27
mytoken
--> 代币名称
2022-08-27
--> 给定令牌的到期日期
大家好,我是 python 的新手,正在尝试调试下面的错误。该脚本基本上在我的 Gitlab 端点上创建了一个 PAT(个人访问令牌)。
Traceback (most recent call last):
File "/Test/test-gitlabtoken.py", line 86, in <module>
main()
File "/Test/test-gitlabtoken.py", line 79, in main
name = sys.argv[1]
IndexError: list index out of range
下面是我从这里提取的完整脚本 --> https://github.com/vitalyisaev2/gitlab_token/blob/main/personal_access_token.py
#!/usr/bin/python3
"""
Script that creates Personal Access Token for Gitlab API;
Tested with:
- Gitlab Community Edition 10.1.4
- Gitlab Enterprise Edition 12.6.2
- Gitlab Enterprise Edition 13.4.4
"""
import sys
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
endpoint = "https://blah.com" # updated
root_route = urljoin(endpoint, "/")
sign_in_route = urljoin(endpoint, "/users/sign_in")
pat_route = urljoin(endpoint, "/-/profile/personal_access_tokens")
login = "root"
password = "pass" # updated
def find_csrf_token(text):
soup = BeautifulSoup(text, "lxml")
token = soup.find(attrs={"name": "csrf-token"})
param = soup.find(attrs={"name": "csrf-param"})
data = {param.get("content"): token.get("content")}
return data
def obtain_csrf_token():
r = requests.get(root_route)
token = find_csrf_token(r.text)
return token, r.cookies
def obtain_authenticity_token(cookies):
r = requests.get(pat_route, cookies=cookies)
soup = BeautifulSoup(r.text, "lxml")
token = soup.find('input', attrs={'name': 'authenticity_token', 'type': 'hidden'}).get('value')
return token
def sign_in(csrf, cookies):
data = {
"user[login]": login,
"user[password]": password,
"user[remember_me]": 0,
"utf8": "✓"
}
data.update(csrf)
r = requests.post(sign_in_route, data=data, cookies=cookies)
token = find_csrf_token(r.text)
return token, r.history[0].cookies
def obtain_personal_access_token(name, expires_at, csrf, cookies, authenticity_token):
data = {
"personal_access_token[expires_at]": expires_at,
"personal_access_token[name]": name,
"personal_access_token[scopes][]": "api",
"authenticity_token": authenticity_token,
"utf8": "✓"
}
data.update(csrf)
r = requests.post(pat_route, data=data, cookies=cookies)
soup = BeautifulSoup(r.text, "lxml")
token = soup.find('input', id='created-personal-access-token').get('value')
return token
def main():
csrf1, cookies1 = obtain_csrf_token()
print("root", csrf1, cookies1)
csrf2, cookies2 = sign_in(csrf1, cookies1)
print("sign_in", csrf2, cookies2)
authenticity_token = obtain_authenticity_token(cookies2)
name = sys.argv[1]
expires_at = sys.argv[2]
token = obtain_personal_access_token(name, expires_at, csrf2, cookies2, authenticity_token)
print(token)
if __name__ == "__main__":
main()
我的 python 版本是 Python 3.9.7
我将不胜感激任何指示。谢谢
搜索错误(源)行时:
name = sys.argv[1]
你可以找到这个: Meaning sys argv
这里的问题是 sys.argv 给出的列表没有提供第二个属性。
如果您使用 Spyder IDE 编码,您可能会在 link 中找到一些调试帮助:
否则您可能会尝试使用 Python 调试器 (Pdb)。
感谢您的反馈。我在命令行中遗漏了一些参数。所以我可以使用下面的命令解锁自己:
python3 personal_access_token.py mytoken 2022-08-27
mytoken
--> 代币名称
2022-08-27
--> 给定令牌的到期日期