Comp 150, Dordal
Connection addresses are pairs (hostname, port), where the hostname
is a string and the port is a number. To connect, 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.
Python uses the parentheses around an address to create a python pair object (or, more generally, a tuple):
you simply type ("ulam2.cs.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(("ulam2.cs.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):Here's what my first few output lines looked like:
addr=("ulam2.cs.luc.edu", portnum)
...