Check if a connection is established on the port

I have to check if the port is open but I'm not sure how to extend this, also check if the connection is established.

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")

      

+3


source to share





All Articles