473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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:"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>"
Oct 24 '08 #1
5 2494
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 consolein <module>()

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

Ciao,
Marc 'BlackJack' Rintsch
Oct 24 '08 #2
On Fri, Oct 24, 2008 at 1:42 PM, asit <li*****@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
Oct 24 '08 #3
On Oct 24, 11:18*pm, "Jerry Hill" <malaclyp...@gmail.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.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$
Oct 24 '08 #4
On Fri, Oct 24, 2008 at 2:33 PM, asit <li*****@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.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/...s/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
Oct 24 '08 #5
In message
<f2**********************************@z18g2000prn. googlegroups.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:"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?
Oct 25 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ben Finney | last post by:
Howdy all, I'm experimenting with carrying my personal computing environment around on a keychain USB flash storage device. I have the usual suspects on there: SSH keys, GPG keys, program...
3
by: Koen Vossen | last post by:
Hi, I have been asked to develop an automated cash register application for use in a typical restaurant. The catch is that it has to run on Windows. My questions: is the python - wxPython...
16
by: Vent d'Est - East Wind | last post by:
i have some question about portability of python for my part i work under the 3 systemes (windows , linux , mac os x sometimes mac os classic too ) i want to know how i can be sure at 100 %...
0
by: Alexander Staubo | last post by:
Python does not seem to clean up gracefully on SIGTERM: The exit sequence is not invoked on termination, so the atexit module cannot be used to install shutdown logic. Further, while the signal...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
0
by: Andreas | last post by:
Hi Group, I want to get into writing portable apps that can run solely off a USB stick, but I have a few problems. I'm originally a java/.net developer, but I don't want it to be a...
6
by: Bart Van Loon | last post by:
Hi all, I'm looking for a portable (FreeBSD and Linux) way of getting typical ifconfig information into Python. Some research on the web brought me to Linux only solutions ...
6
by: Brendan Miller | last post by:
Hi, I have functions that take a file object and write to it. In some cases I just want to throw out what is written to that file object. I want something like open('/dev/null', 'w'), but...
1
by: luismi | last post by:
Hi, I have searched the online manuals, faqs and forums, but i haven't found a satisfactory explanation ... most probably my fault ;) I have found 2 projects, one commercial and another free,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.