Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / names / examples / dns-service.py
1 #!/usr/bin/env python
2
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
5
6 """
7 Sample app to lookup SRV records in DNS.
8 To run this script:
9 $ python dns-service.py <service> <proto> <domain>
10 where,
11 service = the symbolic name of the desired service.
12 proto = the transport protocol of the desired service; this is usually either TCP or UDP.
13 domain =  the domain name for which this record is valid.
14 e.g.:
15 $ python dns-service.py sip udp yahoo.com
16 $ python dns-service.py xmpp-client tcp gmail.com
17 """
18
19 from twisted.names import client
20 from twisted.internet import reactor
21 import sys
22
23 def printAnswer((answers, auth, add)):
24     if not len(answers):
25         print 'No answers'
26     else:
27         print '\n'.join([str(x.payload) for x in answers])
28     reactor.stop()
29
30 def printFailure(arg):
31     print "error: could not resolve:", arg
32     reactor.stop()
33
34 try:
35     service, proto, domain = sys.argv[1:]
36 except ValueError:
37     sys.stderr.write('%s: usage:\n' % sys.argv[0] +
38                      '  %s SERVICE PROTO DOMAIN\n' % sys.argv[0])
39     sys.exit(1)
40
41 resolver = client.Resolver('/etc/resolv.conf')
42 d = resolver.lookupService('_%s._%s.%s' % (service, proto, domain), [1])
43 d.addCallbacks(printAnswer, printFailure)
44
45 reactor.run()