#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    35
    Rep Power
    12

    Noobish socket question


    I feel so ignorant...

    But what's the quickest/easiest way to tell if a socket is connected, i.e., open? There's gotta be some flag I can mask to check, or a really easy way...

    Thanks!

    - tps
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    Here. I suppose you have some socket knowledge so I won't go into much explaining.
    Code:
    import socket
    
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    ip = "127.0.0.1" #change to scan another machine
    port = 10000 #or whatever you wish
    if not s.connect_ex((ip, port)):
      print 'Port open.'
    else:
      print 'Port closed.'
    connect_ex(ip,port) is a simple method that returns 0 if the connection worked or some error error number if it failed.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    35
    Rep Power
    12
    Hey, that's dandy. Because if the socket wasn't open, I wanted to open it anyway

    But just for future reference, does anyone know of a simple test to check if a socket is open (without disconnecting it if it is, or connecting it if it isn't)?

    Thanks for the prompt response, SolarBear =)

    - tps
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could always wrap connect() in a try-except statment, if it doesn't connect at first you can then get a usful error message if you want. Also if you put this inside a while loop you can have it try and connect whenever the connection is dropped.

    Sorry, no example for ya

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    I DO have one example - maybe not what you're looking for, but heh. Here's a simple threaded port scanner I wrote. Nothing fancy - plus it hangs if you specify too many ports - but it works !
    Code:
    # Zero24's single IP port scanner V1.0
    # V1.3.1 by SolarBear
    import socket,thread
    
    def scan_port(ip,port,s):
        global openports,t_number,t_lock,open_lock
        if not s.connect_ex((ip, n)):
            open_lock.acquire()
            openports.append(n)
            open_lock.release()
        s.close()
        t_lock.acquire()
        t_number -= 1
        t_lock.release()
        
    ip = socket.gethostbyname(socket.gethostname()) #local IP
    Sport = input("Please enter the start port number: ")
    Eport = input("Please enter the end port number: ")
    Eport = Eport + 1
    
    print "You are scanning", ip
    print "for ports", Sport,"to", Eport - 1
    
    openports = []
    sockets = [socket.socket(socket.AF_INET,socket.SOCK_STREAM) for i in range(Sport,Eport)]
    open_lock = thread.allocate_lock()
    t_number = Eport - Sport
    t_lock = thread.allocate_lock()
    for n in range(Sport, Eport):
        thread.start_new_thread(scan_port,(ip,n,sockets[n-Sport]))
    while t_number > 0: pass
    if len(openports) > 0:
        openports = sort(openports)
        print "Open ports: ",openports
    else: print "No open ports. Sorry."
    This does what you asked for, though : by calling connect or connect_ex, you try connection to the remote host : if it worked you stay connected and if it didn't... well you obviously can't connect anyway !

IMN logo majestic logo threadwatch logo seochat tools logo