# L-34 MCS 275 Wed 9 Apr 2008 : lookforpyfiles.py

# Prompts the user for a URL and scans the page
# for all strings which end in .py.

import urllib

def PyStrings(f):
   """
   The input is f, a file like object returned
   by the urllib.urlopen command.
   On return is the list of strings that end in .py.
   We assume that the buffer size is large enough
   there is at least one space in every buffer.
   """
   L = []
   last = ''
   while True:
      data = f.read(80)
      if data == '': break
      s = data.split(' ')
      last = last + s[0]
      if last[-3:] == '.py': L.append(last)
      last = ''
      for i in range(1,len(s)-1):
         n = s[i]
         if n[-3:] == '.py': L.append(n)
      last = s[len(s)-1]
      if last[-3:] == '.py':
         L.append(last)
         last = ''
   return L

def main():
   """
   Prompts the user for URL.
   Opens web page and then scans the page
   for strings ending in .py.
   """
   u = raw_input('give a URL : ')
   print 'opening %s ...' % u
   f = urllib.urlopen(u)
   L = PyStrings(f)
   print 'strings ending in .py :'
   print L

if __name__=="__main__": main()
