如果我尝试运行此脚本,则会收到错误消息“ NamError:未定义名称'self'”,请帮助我解决这个问题,这是我不熟悉Python。我尝试通过阅读其他资源来解决此问题,但这没有用。
#!/usr/bin/env python3
import hmac
import hashlib
import base64
import argparse
# Values that are required to calculate the signature. These values should
# never change.
DATE = "11111111"
SERVICE = "ses"
MESSAGE = "SendRawEmail"
TERMINAL = "aws4_request"
VERSION = 0x04
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def calculateKey(secretAccessKey, region):
signature = sign(("AWS4" + secretAccessKey).encode('utf-8'), DATE)
signature = sign(signature, region)
signature = sign(signature, SERVICE)
signature = sign(signature, TERMINAL)
signature = sign(signature, MESSAGE)
signatureAndVersion = bytes([VERSION]) + signature
smtpPassword = base64.b64encode(signatureAndVersion)
print(smtpPassword.decode('utf-8'))
def main(self):
parser = argparse.ArgumentParser(description='Convert a Secret Access Key for an IAM user to an SMTP password.')
parser.add_argument('--secret')
help='my_access_id_here',
required=True,
action="store"
parser.add_argument('--region')
help='us-west-2',
required=True,
choices=['us-east-1','us-west-2','eu-west-1'],
action="store"
args = parser.parse_args()
calculateKey(args.secret,args.region)
main(self)
答案 0 :(得分:0)
您需要从main()中删除self,因为只有当self是类方法时才需要self,常规函数没有以self作为第一个参数。
文档引用:
通常,方法的第一个参数称为self。这无非是一种约定:self本身对Python绝对没有特殊含义。
有关更多信息,请参见下面的文档