Source code for lec13_files_from_web
""" Illustrates the use of HTML files over the web in programs
Files from the web are opened using the urllib library
After this, the file is processed in the same way as a file
on your own hard disk.
Note we are using different methods to read the files
but they are all equivalent and are used to illustrate the
different methods
"""
import urllib
[docs]def is_palindrome(word):
""" Returns True if the word is a palindrome, the word is the
same when read forward and backwards.
It returns False otherwise.
"""
word = word.strip()
word = word.lower()
for i in range( len(word)/2 ):
if word[i] != word[-i-1]:
return False ## if a mismatch is found, return False
return True # if no mismatch is found, return True
###########
if __name__ == "__main__":
word_url = 'http://thinkpython.com/code/words.txt'
word_file = urllib.urlopen(word_url)
i = 0
while True:
i += 1
word = word_file.readline()
if word == '': ## this is true when the end of file is reached
break
if is_palindrome(word):
print word.strip()