How to handle errors?

SS sami.strat at gmail.com
Thu Oct 20 15:48:28 EDT 2016


The following script works fine:

#!/bin/python

import socket

str = raw_input("Enter a domain name: ");
print "Your domain is ", str
print socket.gethostbyname(str)

You provide it a hostname, it provides an IP.  That works fine.  But I need a way to handle errors.  For example:

[root at bart /]# ./dataman.py
Enter a domain name: aslfhafja
Your domain is  aslfhafja
Traceback (most recent call last):
  File "./dataman.py", line 7, in <module>
    print socket.gethostbyname(str)
socket.gaierror: [Errno -2] Name or service not known
[root at bart /]#

I would like to be able to handle that error a bit better.  Any ideas?

TIA. 



More information about the Python-list mailing list