portable python
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Fri Oct 24 14:07:48 EDT 2008
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,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
More information about the Python-list
mailing list