Revision 25745,
1.4 KB
checked in by jdreed, 12 years ago
(diff) |
In athinfo:
* Catch IOErrors (Trac #1214)
* Use .writelines() instead of .write() with .read()
* Bump Standards-Version
|
-
Property svn:executable set to
*
|
Line | |
---|
1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import optparse |
---|
4 | import os |
---|
5 | import signal |
---|
6 | import socket |
---|
7 | import sys |
---|
8 | |
---|
9 | ATHINFO_FALLBACK_PORT = 49155 |
---|
10 | |
---|
11 | def alarm(signum, frame): |
---|
12 | print >>sys.stderr, 'athinfo: timed out' |
---|
13 | sys.exit(1) |
---|
14 | |
---|
15 | def main(): |
---|
16 | parser = optparse.OptionParser( |
---|
17 | usage='%prog [options] host query' |
---|
18 | ) |
---|
19 | parser.add_option( |
---|
20 | '-t', '--timeout', |
---|
21 | type='int', |
---|
22 | dest='timeout', |
---|
23 | help='timeout after SECONDS', |
---|
24 | metavar='SECONDS', |
---|
25 | ) |
---|
26 | |
---|
27 | (options, args) = parser.parse_args() |
---|
28 | if len(args) != 2: |
---|
29 | parser.print_usage() |
---|
30 | return 1 |
---|
31 | host = args[0] |
---|
32 | query = args[1] |
---|
33 | |
---|
34 | try: |
---|
35 | port = socket.getservbyname('athinfo', 'tcp') |
---|
36 | except socket.error: |
---|
37 | port = ATHINFO_FALLBACK_PORT |
---|
38 | |
---|
39 | try: |
---|
40 | host_addr = socket.gethostbyname(host) |
---|
41 | except socket.error: |
---|
42 | print >>sys.stderr, 'athinfo: host %s not found' % host |
---|
43 | return 1 |
---|
44 | |
---|
45 | try: |
---|
46 | s = socket.socket() |
---|
47 | |
---|
48 | if options.timeout: |
---|
49 | signal.signal(signal.SIGALRM, alarm) |
---|
50 | signal.alarm(options.timeout) |
---|
51 | |
---|
52 | s.connect((host_addr, port)) |
---|
53 | s.sendall('%s\n' % query) |
---|
54 | sys.stdout.writelines(s.makefile()) |
---|
55 | except socket.error, e: |
---|
56 | print >>sys.stderr, 'athinfo: %s' % e.args[-1] |
---|
57 | return 1 |
---|
58 | except IOError, e: |
---|
59 | print >>sys.stderr, 'athinfo: %s' % e.args[-1] |
---|
60 | return 1 |
---|
61 | except KeyboardInterrupt: |
---|
62 | return 1 |
---|
63 | |
---|
64 | |
---|
65 | if __name__ == '__main__': |
---|
66 | sys.exit(main()) |
---|
Note: See
TracBrowser
for help on using the repository browser.