portable python

A

asit

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:"address not available",10061:"closed",
10060:"timeout",10056:"already connected",10035:"filtered",11001:"IP
not found",10013:"permission denied"}

def scan(ip,port,timeout):
s = socket(AF_INET, SOCK_STREAM)
s.settimeout(timeout)
try:
result= s.connect_ex((ip, 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(minrange,maxrange+1)

for port in ports:
print str(port) + " : " + scan(ip,port,timeout)
else:
print "usage : " + sys.argv[0] + " <ip-address> <min-port
range> <max-port range>"
 
M

Marc 'BlackJack' Rintsch

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,timeout):
s = socket(AF_INET, SOCK_STREAM)
s.settimeout(timeout)
try:
result= s.connect_ex((ip, 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`, `KeyboardInterrupt`, …

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(minrange,maxrange+1)

for port in ports:
print str(port) + " : " + scan(ip,port,timeout)

…one of which is `None` and that will blow up here, regardless of
platform.

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

/home/bj/<ipython console> in <module>()

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

Ciao,
Marc 'BlackJack' Rintsch
 
J

Jerry Hill

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.
 
A

asit

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.

this the o/p
lipu@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@lipu-desktop:~/hack$
 
J

Jerry Hill

this the o/p
lipu@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@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)
 
L

Lawrence D'Oliveiro

In message
asit said:
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:"address not available",10061:"closed",
10060:"timeout",10056:"already connected",10035:"filtered",11001:"IP
not found",10013:"permission denied"}

I think these numbers are Dimdows error codes?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top