检查端口是否建立了连接

时间:2014-10-17 10:15:17

标签: python sockets

我要检查端口是否打开,但我不知道如何扩展它也检查连接是否已建立。

import socket

ip_address_machine='127.0.0.1'
port_to_try=3389

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = s.connect_ex((ip_address_machine, port_to_try))
    s.close()
    if(result == 0) :
        print ("Port ",port_to_try, "is open on machine ", ip_address_machine)
    else:
        print ("Port ",port_to_try, "is not open on machine ", ip_address_machine)
except:
    print("Machine is off")

0 个答案:

没有答案