summaryrefslogtreecommitdiffstats
path: root/Demo/tkinter/www/www3.py
blob: e1b1bc5b4be84300842843f549fa9db4718101dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env python

# www3.py -- print the contents of a URL on stdout
# - error checking
# - Error 302 handling

import sys
import urllib
import types

def main():
	if len(sys.argv) != 2 or sys.argv[1][:1] == '-':
		print "Usage:", sys.argv[0], "url"
		sys.exit(2)
	url = sys.argv[1]
	fp = my_urlopen(url)
	while 1:
		line = fp.readline()
		if not line: break
		sys.stdout.write(line)

def my_urlopen(url):
	try:
		fp = urllib.urlopen(url)
		return fp
	except IOError, msg:
		if type(msg) == types.TupleType and len(msg) == 4:
			m = msg[3]
			if msg[1] == 302:
				if m.has_key('location'):
					url = m['location']
					print 'Location:', url
					return my_urlopen(url)
				elif m.has_key('uri'):
					url = m['uri']
					print 'URI:', url
					return my_urlopen(url)
				print '(Error 302 w/o Location/URI header???)'
			print msg[:3]
			for line in m.headers:
				sys.stdout.write(line)
		else:
			print msg
		sys.exit(1)

main()