portable python

Jerry Hill malaclypse2 at gmail.com
Fri Oct 24 15:35:37 EDT 2008


On Fri, Oct 24, 2008 at 2:33 PM, asit <lipun4u at gmail.com> wrote:
> this the o/p
> lipu at lipu-desktop:~/hack$ python portscan.py 59.93.128.10 10 20
> Traceback (most recent call last):
>  File "portscan.py", line 33, in <module>
>    print str(port) + " : " + scan(ip,port,timeout)
>  File "portscan.py", line 22, in scan
>    return status[result]
> KeyError: 11
> lipu at lipu-desktop:~/hack$

Oh, connect_ex is returning errno 11, which isn't in your dictionary
of statuses.  Did you think that the eight items in your status
dictionary were the only possible return values of connect_ex?  Since
the socket module is a thin wrapper over the c socket library, you'll
probably need to consult the documentation for that to see exactly
what's going on.  I'd start with "man connect" on your unix command
line, or this page:
http://www.opengroup.org/onlinepubs/009695399/functions/connect.html

You'd probably be better off using built in modules to map errno to a
message, like this:

from socket import *
import os

def scan(ip, port, timeout):
    s = socket(AF_INET, SOCK_STREAM)
    s.settimeout(timeout)
    errno = s.connect_ex((ip, port))
    return os.strerror(errno)



-- 
Jerry



More information about the Python-list mailing list