telnet 网络连通性
telnetlib 模块提供了一个实现 Telnet 协议的 Telnet 类。
示例1:
from telnetlib import Telnet
try:
with Telnet('localhost',543) as tn:
print(111111)
except Exception as e:
print(e) # [WinError 10061] 由于目标计算机积极拒绝,无法连接。
示例2:
import telnetlib
def telnetFunc(host, port, timeout):
tnl = telnetlib.Telnet()
try:
tnl.open(host, port=port, timeout=timeout)
except Exception as e:
print(f"{e}:请检查网络或防火墙是否已开通!")
finally:
tnl.close()
if __name__ == '__main__':
telnetFunc('192.168.35.12', 1000, 3)