query a port

Dan M dan at wolf.com
Sun Oct 30 01:15:00 EDT 2005


On Sat, 29 Oct 2005 20:21:20 -0700, eight02645999 wrote:

> hi
> in python, how do one query a port to see whether it's up or not?
> thanks

I'm an absolute beginner, but let's see if I can help. Assuming you want
to check a port on another machine,

import socket
port=25			# Port we want to test
host=machine.to.test	# Either IP address or FQDN
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
try:
	s.connect((host, port))	
	print "We made the connection"
except socket.error:
	print "Sorry, that port is not open"
s.close

Does that help?

Dan



More information about the Python-list mailing list