The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Noobish socket question
Discuss Noobish socket question in the Python Programming forum on Dev Shed. Noobish socket question Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 13th, 2004, 09:40 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 35
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
|

January 13th, 2004, 09:53 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
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.
|

January 13th, 2004, 09:57 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 35
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
|

January 14th, 2004, 02:29 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

January 14th, 2004, 02:55 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
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 !
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|