Skip to main content
 首页 » 编程设计

python之使用 python 检查重启后 AWS 实例是否启动

2025年05月04日13zfyouxi

有没有办法使用 boto3 或其他方式检查 AWS 实例是否最终出现在 python 中。运行状态不区分重启和最终启动阶段。

请您参考如下方法:

如果您只想检查远程端口是否打开,您可以使用内置的 socket包。

这是对 this answer 的快速修改等待远程端口打开:

import socket 
import time 
 
def wait_for_socket(host, port, retries, retry_delay=30): 
    retry_count = 0 
    while retry_count <= retries: 
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
        result = sock.connect_ex((host, port)) 
        sock.close() 
        if result == 0: 
            print "Port is open" 
            break 
        else: 
            print "Port is not open, retrying..." 
            time.sleep(retry_delay)