""" Some basic functionality for reading data from web pages """ # David Kauchak # 11/8/2011 import urllib import sys def print_data(reader): """Print the data a line at a time from the iterable reader""" for line in reader: print line.strip() def is_url(string): """See if this string represents a URL""" return string.startswith("http://") def print_usage(): """Print the usage of this program""" print "url_basics.py " if __name__ == "__main__": # check to see if the user called the program with the right number # of command-line arguments if len(sys.argv) != 2: print_usage() else: # get the argument the user called the program with name = sys.argv[1] if is_url(name): reader = urllib.urlopen(name) else: reader = open(name, "r") print_data(reader)