|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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 ! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Noobish socket question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|