How to handle errors?
Wildman
best_lay at yahoo.com
Thu Oct 20 18:31:41 EDT 2016
On Thu, 20 Oct 2016 12:48:28 -0700, SS wrote:
> 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.
#!/usr/bin/env python
import socket
def get_ip(url):
try:
ip = socket.gethostbyname(url)
return ip
except socket.gaierror:
return "***ERROR***\nUnknown domain name!"
domain = raw_input("Enter a domain name: ");
print "Your domain is ", domain
print get_ip(domain)
--
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!
More information about the Python-list
mailing list