Ticket #5596: gethostbyname-exit-5596-2.patch

File gethostbyname-exit-5596-2.patch, 2.9 KB (added by Richard Wall, 8 years ago)

added better error reporting

  • doc/names/examples/gethostbyname.py

    === modified file 'doc/names/examples/gethostbyname.py'
     
    44# See LICENSE for details.
    55
    66"""
    7 Returns the IP address for a given hostname.
    8 To run this script:
    9 $ python gethostbyname.py <hostname>
    10 e.g.:
    11 $ python gethostbyname.py www.google.com
     7USAGE: python gethostbyname.py HOSTNAME
     8
     9Returns the IP address for a given hostname. eg
     10
     11$ python gethostbyname.py www.google.com
     122a00:1450:400c:c05::67
     13$ python gethostbyname.py www.google.com
     14173.194.67.104
     15$ python gethostbyname.py localhost
     16127.0.0.1
     17$ python gethostbyname.py com
     18ERROR: No IP adresses found for hostname 'com'
     19$ python gethostbyname.py does.not.exist
     20ERROR: hostname not found 'does.not.exist'
     21
     22This script does a host lookup using the default Twisted Names
     23resolver.
     24
     25The default Twisted Names resolver is a chained resolver, which
     26attempts to lookup a name using:
     27 * local hosts file
     28 * memory cache of previous lookup results
     29 * system wide recursive DNS servers
     30
     31The hosts file lookup is synchronous, which means that the deferred
     32response can run its callbacks before the reactor is started.
     33
     34To ensure that the reactor is started and stopped in the correct
     35order, we use reactor.callWhenRunning to postpone the host lookup
     36until after the reactor has started.
    1237"""
    1338import sys
    14 from twisted.names import client
     39from twisted.names import client, error
    1540from twisted.internet import reactor
    1641
    17 def gotResult(result):
    18     print result
    19     reactor.stop()
    20 
    21 def gotFailure(failure):
    22     failure.printTraceback()
    23     reactor.stop()
    24 
    25 d = client.getHostByName(sys.argv[1])
    26 d.addCallbacks(gotResult, gotFailure)
    27 
     42
     43def gotResult(result, hostname):
     44    """
     45    Print the IP address or an error message if an IP address was not
     46    found.
     47    """
     48    if result:
     49        print result
     50    else:
     51        sys.stderr.write(
     52            'ERROR: No IP adresses found for hostname %r\n' % (hostname,))
     53
     54
     55def gotFailure(failure, hostname):
     56    """
     57    Print a friendly error message if the hostname could not be
     58    resolved.
     59    """
     60    errorClass = failure.trap(error.DNSNameError)
     61    sys.stderr.write('ERROR: hostname not found %r\n' % (hostname,))
     62
     63
     64def startLookup(hostname):
     65    d = client.getHostByName(hostname)
     66    d.addCallback(gotResult, hostname)
     67    d.addErrback(gotFailure, hostname)
     68    # Print full traceback for any uncaught errors before shutting
     69    # down the reactor
     70    d.addErrback(lambda failure: failure.printTraceback())
     71    d.addBoth(lambda ign: reactor.stop())
     72
     73try:
     74    hostname = sys.argv[1]
     75except IndexError:
     76    sys.stderr.write(
     77        __doc__ + '\n' +
     78        'ERROR: Missing HOSTNAME argument\n')
     79    raise SystemExit(1)
     80
     81reactor.callWhenRunning(startLookup, hostname)
    2882reactor.run()