Skip to content

added python 3 support #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

Revision history
~~~~~~~~~~~~~~~~
January 30 2015
changes by Paul Hope:
- added python 3 support for exceptions and print()
- changed xrange() to python 3 range()

March 11, 2010
changes by Samuel Stauffer:
Expand Down Expand Up @@ -176,14 +180,14 @@ def do_one(dest_addr, timeout):
icmp = socket.getprotobyname("icmp")
try:
my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
except socket.error, (errno, msg):
if errno == 1:
except OSError as e:
if e.errno == 1:
# Operation not permitted
msg = msg + (
msg = str(e) + str(
" - Note that ICMP messages can only be sent from processes"
" running as root."
)
raise socket.error(msg)
raise OSError(msg)
raise # raise the original error

my_ID = os.getpid() & 0xFFFF
Expand All @@ -200,20 +204,20 @@ def verbose_ping(dest_addr, timeout = 2, count = 4):
Send >count< ping to >dest_addr< with the given >timeout< and display
the result.
"""
for i in xrange(count):
print "ping %s..." % dest_addr,
for i in range(count):
print( "ping %s..." % dest_addr,)
try:
delay = do_one(dest_addr, timeout)
except socket.gaierror, e:
print "failed. (socket error: '%s')" % e[1]
except socket.gaierror as e:
print("failed. (socket error: '%s')" % e[1])
break

if delay == None:
print "failed. (timeout within %ssec.)" % timeout
print("failed. (timeout within %ssec.)" % timeout)
else:
delay = delay * 1000
print "get ping in %0.4fms" % delay
print
print("get ping in %0.4fms" % delay)
print('')


if __name__ == '__main__':
Expand Down