任何端口问题:Python挂在STSConnection()上。在服务器上获取\u会话\u令牌,但在我的笔记本电脑上工作正常

2024-04-23 19:17:29 发布

您现在位置:Python中文网/ 问答频道 /正文

当我从:膝上型电脑运行时,没有问题,但当我从我公司的服务器运行时,它挂在STSConnection()。获取\u会话\u令牌

这是tempCredentials=sts吗_connection.get\会话\令牌需要打开任何端口

 import boto
 import datetime
 from datetime import date, timedelta
 import subprocess
 import os
 import argparse
 from boto.s3.connection import S3Connection
 from boto.sts import STSConnection
 import shutil





 #command line arguments
 parser = argparse.ArgumentParser(description='To create Temp  credentials through STS function and upload to ~/.aws/credentials')

 parser.add_argument('-d', '--device_id', help='acc  xxxxxxxx neumerical value', required=True)
 parser.add_argument('-u', '--user_id', help='user id like xxx  ', required=True)
 parser.add_argument('-p', '--parent_profile', help='parent profile ', required=True)
 parser.add_argument('-m', '--mfa_profile', help='profile', required=True)

 args = parser.parse_args()
  deviceId = args.device_id
  userID   = args.user_id
  parentProfile = args.parent_profile
  mfaProfile = args.mfa_profile



 # Prompt for MFA time-based one-time password (TOTP)
 mfa_TOTP = raw_input("Enter the MFA code: ")

 print "STS connection"
 #sts connection
 sts_connection = STSConnection(profile_name=parentProfile)


 print "STS connection  temp credentials"
 tempCredentials = sts_connection.get_session_token(
    duration=43200,
    mfa_serial_number="arn:aws:iam::" + deviceId + ":mfa/" + userID,
    mfa_token=mfa_TOTP
 )
 print "STS connection  temp credentials closed"

 print str(tempCredentials.access_key)

Tags: importmfaaddidtrueparserrequiredhelp
1条回答
网友
1楼 · 发布于 2024-04-23 19:17:29

AWS实现了restapi。这意味着端口443需要对外开放。返回端口可以是1024到65534之间的任意值。你知道吗

尝试使用wget或curl对一个知名网站(https://www.amazon.com)发出一个普通的HTTPS请求,并确保这个请求没有被阻止。如果返回一个有效的网页,那么awsrestapi就可以了。你知道吗

再次检查您是否正在使用STS的全局enpoint。此链接将为您提供更多信息。尝试wget https://sts.amazonaws.com以确保STS端点响应。你知道吗

Activating and Deactivating AWS STS in an AWS Region

最后不要使用你的根凭据。创建IAM用户以分配临时凭据。你知道吗

使用boto3的最终溶液:

   import boto3
   #sts connection
   session = boto3.Session(profile_name=parentProfile)
   sts_client = session.client('sts')

   tempCredentials = sts_client.get_session_token(
   DurationSeconds=43200,
   SerialNumber="arn:aws:iam::" + deviceId + ":mfa/" + userID,
   TokenCode=mfa_TOTP
   )
   print "STS connection  temp credentials closed"
   print(tempCredentials)

相关问题 更多 >