【发布时间】:2021-12-14 10:24:19
【问题描述】:
以下代码在 Amazon Linux 2 上运行时会产生此错误。目的是让 python 程序响应来自不和谐服务器的用户输入。我知道的足以让我了解python。任何帮助表示赞赏。他们的服务器正在运行更新版本的 Amazon Linux 2。
File "./bot.py", line 65, in <module>
client.run(os.environ[config.discord_key])
File "/usr/lib64/python3.7/os.py", line 681, in __getitem__
raise KeyError(key) from None
KeyError: 'SuperSecretSquirrelStuff-key'
import discord, asyncio, os, boto3, config
client = discord.Client()
ec2 = boto3.resource('ec2')
instance_id = config.instance_id
#Temp
instance = ec2.Instance(instance_id)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------------')
@client.event
async def on_message(message):
memberIDs = (member.id for member in message.mentions)
if client.user.id in memberIDs:
if 'stop' in message.content:
if turnOffInstance():
await client.send_message(message.channel, 'AWS Instance stopping')
else:
await client.send_message(message.channel, 'Error stopping AWS Instance')
elif 'start' in message.content:
if turnOnInstance():
await client.send_message(message.channel, 'AWS Instance starting')
else:
await client.send_message(message.channel, 'Error starting AWS Instance')
elif 'state' in message.content:
await client.send_message(message.channel, 'AWS Instance state is: ' + getInstanceState())
elif 'reboot' in message.content:
if rebootInstance():
await client.send_message(message.channel, 'AWS Instance rebooting')
else:
await client.send_message(message.channel, 'Error rebooting AWS Instance')
def turnOffInstance():
try:
instance.stop(False, False)
return True
except:
return False
def turnOnInstance():
try:
instance.start()
return True
except:
return False
def getInstanceState():
return instance.state['Name']
def rebootInstance():
try:
instance.reboot()
return True
except:
return False
client.run(os.environ[config.discord_key])```
【问题讨论】:
-
是否设置了环境变量
SuperSecretSquirrelStuff-key? -
我还有一个名为 config.py 的文件,其中包含一个设置为 instance_id 和 discord_key 的变量。这里的 SSSS-key 是我的实际令牌的占位符,它存储在 config.py 中并返回错误。
标签: python linux amazon-ec2 keyerror