Skip to content

Instantly share code, notes, and snippets.

@winny-
Last active August 29, 2015 14:04
Show Gist options
  • Save winny-/a34576a40f4918812d8d to your computer and use it in GitHub Desktop.
Save winny-/a34576a40f4918812d8d to your computer and use it in GitHub Desktop.
ipv4 only
#!/usr/bin/env python
import socket
import sys
def main(host):
exit_code = 0
try:
# This is a work-around for socket.gethostbyname() causing KeyboardInterrupt to be uncatchable.
# From: https://mail.python.org/pipermail/python-bugs-list/2007-July/039118.html
try:
address = socket.gethostbyname(host)
except KeyboardInterrupt:
raise
print(address)
except (socket.gaierror, KeyboardInterrupt):
exit_code = 1
sys.exit(exit_code)
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment