query a port

D

Dan M

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
 
D

Dan M

thanks alot!
that's all there is to it..so it's just a simple connect.

If all you want to do is check that the given port is open on the given
host, that's it. I tried it on my local box. When connecting to port 25,
it made the connection fine. Trying to connect to port 26 raised a
socket.error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,028
Latest member
chasetony

Latest Threads

Top