Comp 150, Dordal, Mon, Apr 10, 2006 (swapped from Fri Apr 7)
To connect to an address (see the next paragraph), use
s.connect(address)
or
s.connect_ex(address)
If a connection fails (eg because the server isn't listening at
the specified address), the latter returns a numeric value != 0
while the former throws an exception which is harder to
deal with within a loop or some larger program.
To check the numeric value, use
n=s.connect_ex(address) and then check n.
Addresses are pairs (hostname,port). The hostname is a string,
and the ports we're interested in are in the range 2001-2030.
The use of () to create a python pair object is likely new to you:
you simply type ("ulam.math.luc.edu", 2001). The catch is that these
parentheses are part of the pair, and you need a second set of
parentheses for the connect call:
n=s.connect_ex(("ulam.math.luc.edu",2001))
To read 1024 bytes of data from a connection
(none of the servers we'll be accessing has more data than that), use
s.recv(1024)
You need to print this; use the following:
str = s.recv(1024)
print str
It's probably a good idea to print a header too, like
print "Port", portnum
for portnum in range(2001,2031): addr=("ulam.math.luc.edu", portnum) ...Email me your python file, or show it to me.