[Tutor] Dodgey if loop ?

nick at javacat.f2s.com nick at javacat.f2s.com
Tue Sep 7 10:04:23 CEST 2004


Thankyou.

I have since changed the code to use a dict.
However, I'm getting the same error, Im quite confused :(

The dictionary portDict below is of the form {port_nr:port_name}.

here's the code:
[code]
     78 for port in range(sport, eport): # ie range(10,51)
     79     try:
     80         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     81         s.settimeout(2)
     82         s.connect((host, port))
     83         print "\n", host, 'is listening on port', port,
     84         if portDict.has_key(port):
     85             print "(%s)" %portDict[port],
     86         else:
     87             print "Unknown port",
     88         s.close()
     89
     90     except socket.error:
     91         nogood.append(port)
[/code]

The else: on line 86 is always true.
Even if I get rid of the else and change it to

     86         if not portDict.has_key(port):
     87             print "Unknown port",

It is still always returning "Unknown port".

Here's a run with the 'else' or 'if not portDict.has_key()':

[nickl at netview socket]$ ./scan.py bishop1 10 50
Trying bishop1 ...

bishop1 is listening on port 13 Unknown port
bishop1 is listening on port 19 Unknown port
bishop1 is listening on port 21 Unknown port
bishop1 is listening on port 23 Unknown port
bishop1 is listening on port 25 Unknown port
bishop1 is listening on port 37 Unknown port
35 ports were closed

And here's a run with lines 86 and 87 just deleted:

[nickl at netview socket]$ ./scan.py bishop1 10 50
Trying bishop1 ...

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
bishop1 is listening on port 37
35 ports were closed

I believe the answer may of been given to me in a previous reply, but I still
can't get it working.

I apologise for not 'getting' this.

Regards
Nick.




Quoting Roger Merchberger <zmerch at 30below.com>:

> Rumor has it that nick at javacat.f2s.com may have mentioned these words:
> [snip]
>
> Others have mentioned a problem with your algorithm, but there is an
> indentation problem with your code:
>
> >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:
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> This line should not be indented -- and *might* cause problems, tho
> I did not test the code...
>
> >                 return line.split()[0] # would return 'smtp'
> >             #else:
> >                 #return '?'
> >[/code]
>
> Hope this helps,
> Roger "Merch" Merchberger
>
> --
> Roger "Merch" Merchberger   | A new truth in advertising slogan
> sysadmin, Iceberg Computers | for MicroSoft: "We're not the oxy...
> zmerch at 30below.com          |                         ...in oxymoron!"
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



 
-------------------------------------------------
Everyone should have http://www.freedom2surf.net/


More information about the Tutor mailing list