[Tutor] Dodgey if loop ?
Kent Johnson
kent_johnson at skillsoft.com
Mon Sep 6 16:54:14 CEST 2004
The problem is that you are returning too soon. The first time you find p
!= pnumber you will return ?. For example if /etc/services contains entries
for ports 7 and 25, when you see the 7 you will return ?.
You need to put the return '?' after the for loop finishes, so it won't be
executed until the loop has completed without finding the pnumber you are
looking for.
Kent
At 03:25 PM 9/6/2004 +0100, nick at javacat.f2s.com wrote:
>Hi folks,
>
>I've just wrote a little port scanner, and strangely enough I only have one
>problem with it lol .
>
>Here's the offending code:
>
>[code]
>pnumber = str(pnumber) #pnumber is 25 for example
>for line in f: # f is '/etc/services' open file
> if not line.startswith('#') and len(line) > 10:
> p = line.split()[1].split('/')[0]
> if p == pnumber:
> return line.split()[0] # would return 'smtp'
> #else:
> #return '?'
>[/code]
>
>As it stands this works fine.
>However, if I uncomment the else loop in the final if loop it will always
>return
>the '?'. Leaving the final else commented out (as above) if pnumber is not in
>/etc/services it returns 'None' which I can live with.
>
>Can anyone see why this is happening please ?
>
>Here's a brief session of running the program with the final else
>commented out:
>[nickl at netview socket]$ ./scan.py bishop1 1 1024
>Trying bishop1 ...
>
>bishop1 is listening on port 7 (echo)
>bishop1 is listening on port 9 (discard)
>bishop1 is listening on port 13 (daytime)
>bishop1 is listening on port 19 (chargen)
>bishop1 is listening on port 21 (ftp)
>bishop1 is listening on port 23 (telnet)
>bishop1 is listening on port 25 (smtp) ... etc
>
>Here's what happens when I uncomment the final else:
>[nickl at netview socket]$ ./scan.py bishop1 1 1024
>Trying bishop1 ...
>
>bishop1 is listening on port 7 (?)
>bishop1 is listening on port 9 (?)
>bishop1 is listening on port 13 (?)
>bishop1 is listening on port 19 (?)
>bishop1 is listening on port 21 (?)
>bishop1 is listening on port 23 (?)
>bishop1 is listening on port 25 (?) ... etc
>
>Many thanks
>Nick.
>
>
>
>
>
>
>
>
>
>-------------------------------------------------
>Everyone should have http://www.freedom2surf.net/
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list