Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 13th, 2004, 09:40 PM
theperfectsoup theperfectsoup is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 35 theperfectsoup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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

Reply With Quote
  #2  
Old January 13th, 2004, 09:53 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
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.

Reply With Quote
  #3  
Old January 13th, 2004, 09:57 PM
theperfectsoup theperfectsoup is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 35 theperfectsoup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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

Reply With Quote
  #4  
Old January 14th, 2004, 02:29 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #5  
Old January 14th, 2004, 02:55 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
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 !

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Noobish socket question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap