远程服务器的IP地址是指用于在互联网上唯一标识一台远程计算机的数字地址。它遵循IPv4或IPv6协议标准,通常以点分十进制(如192.168.1.1)或冒号分隔的十六进制(如2001:0db8:85a3:0000:0000:8a2e:0370:7334)形式表示。
import socket
def check_server_status(ip_address, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2) # 设置超时时间
result = sock.connect_ex((ip_address, port))
if result == 0:
print(f"Server {ip_address}:{port} is up and running.")
else:
print(f"Server {ip_address}:{port} is down or unreachable.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
sock.close()
# 使用示例
check_server_status("192.168.1.1", 22) # 检查IP地址为192.168.1.1,端口为22的服务器状态
领取专属 10元无门槛券
手把手带您无忧上云