portable python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • asit

    portable python

    I code in both windows and Linux. As python is portable, the o/p
    should be same in both cases. But why the following code is perfect in
    windows but error one in Linux ???

    from socket import *
    import sys

    status={0:"open ",10049:"addres s not available",1006 1:"closed",
    10060:"timeout" ,10056:"already connected",1003 5:"filtered",11 001:"IP
    not found",10013:"p ermission denied"}

    def scan(ip,port,ti meout):
    s = socket(AF_INET, SOCK_STREAM)
    s.settimeout(ti meout)
    try:
    result= s.connect_ex((i p, port))
    except:
    print "Cannot connect to IP"
    return
    s.close()
    return status[result]

    if (len(sys.argv) == 4):
    ip=sys.argv[1]
    minrange = int(sys.argv[2])
    maxrange = int(sys.argv[3])
    timeout = 3

    ports=range(min range,maxrange+ 1)

    for port in ports:
    print str(port) + " : " + scan(ip,port,ti meout)
    else:
    print "usage : " + sys.argv[0] + " <ip-address<min-port
    range<max-port range>"
  • Marc 'BlackJack' Rintsch

    #2
    Re: portable python

    On Fri, 24 Oct 2008 10:42:21 -0700, asit wrote:
    I code in both windows and Linux. As python is portable, the o/p should
    be same in both cases. But why the following code is perfect in windows
    but error one in Linux ???
    So what *is* the error on Linux!?
    def scan(ip,port,ti meout):
    s = socket(AF_INET, SOCK_STREAM)
    s.settimeout(ti meout)
    try:
    result= s.connect_ex((i p, port))
    except:
    print "Cannot connect to IP"
    return
    s.close()
    return status[result]
    The bare ``except`` catches *all* errors in the ``try`` block, even those
    you might know about because they don't belong to the set of exceptions
    you expected. Like `NameError`, `MemoryError`, `KeyboardInterr upt`, …

    And the function can return two quite different types…
    if (len(sys.argv) == 4):
    ip=sys.argv[1]
    minrange = int(sys.argv[2])
    maxrange = int(sys.argv[3])
    timeout = 3
    >
    ports=range(min range,maxrange+ 1)
    >
    for port in ports:
    print str(port) + " : " + scan(ip,port,ti meout)
    …one of which is `None` and that will blow up here, regardless of
    platform.

    In [18]: " : " + None
    ---------------------------------------------------------------------------
    <type 'exceptions.Typ eError' Traceback (most recent call
    last)

    /home/bj/<ipython consolein <module>()

    <type 'exceptions.Typ eError'>: cannot concatenate 'str' and 'NoneType'
    objects

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Jerry Hill

      #3
      Re: portable python

      On Fri, Oct 24, 2008 at 1:42 PM, asit <lipun4u@gmail. comwrote:
      I code in both windows and Linux. As python is portable, the o/p
      should be same in both cases. But why the following code is perfect in
      windows but error one in Linux ???
      What error message do you get in linux? How are you running your code
      in linux? Your code seems to generally work on my Ubuntu linux box,
      so you need to give us more information.

      --
      Jerry

      Comment

      • asit

        #4
        Re: portable python

        On Oct 24, 11:18 pm, "Jerry Hill" <malaclyp...@gm ail.comwrote:
        On Fri, Oct 24, 2008 at 1:42 PM, asit <lipu...@gmail. comwrote:
        I code in both windows and Linux. As python is portable, the o/p
        should be same in both cases. But why the following code is perfect in
        windows but error one   in Linux ???
        >
        What error message do you get in linux?  How are you running your code
        in linux?  Your code seems to generally work on my Ubuntu linux box,
        so you need to give us more information.
        >
        --
        Jerry
        this the o/p
        lipu@lipu-desktop:~/hack$ python portscan.py 59.93.128.10 10 20
        Traceback (most recent call last):
        File "portscan.p y", line 33, in <module>
        print str(port) + " : " + scan(ip,port,ti meout)
        File "portscan.p y", line 22, in scan
        return status[result]
        KeyError: 11
        lipu@lipu-desktop:~/hack$

        Comment

        • Jerry Hill

          #5
          Re: portable python

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


          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(ti meout)
          errno = s.connect_ex((i p, port))
          return os.strerror(err no)



          --
          Jerry

          Comment

          • Lawrence D'Oliveiro

            #6
            Re: portable python

            In message
            <f2e02b13-c6ff-485d-8c88-c03e34a40ea1@z1 8g2000prn.googl egroups.com>, asit
            wrote:
            from socket import *
            I think I'd make it a policy not to help with any scripts that contain
            wildcard imports.
            status={0:"open ",10049:"addres s not available",1006 1:"closed",
            10060:"timeout" ,10056:"already connected",1003 5:"filtered",11 001:"IP
            not found",10013:"p ermission denied"}
            I think these numbers are Dimdows error codes?

            Comment

            Working...