Python Script to Download Apache Logs from a directory on a remote site.


"""
This script is used to get a apache log directory
and download each file from a list of links



"""


import urllib

from sgmllib import SGMLParser

class URLLister(SGMLParser):
    def reset(self):                              
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):                     
        href = [v for k, v in attrs if k=='href']  
        if href:
            self.urls.extend(href)

def getTheFile(n): 
 f = urllib.urlopen("http://www.mywebserver.com/logs/" + n)
 fnew = open ( n , 'w') 
 print ( "getting file: " + n) 
 fnew.write ( f.read() )
 fnew.close()



usock = urllib.urlopen ("http://www.mywebserver.com/logs/")
parser = URLLister()
parser.feed(usock.read())
usock.close()
parser.close()

for url in parser.urls: 
 if (url.find(".log") != -1):
  print url
  s= url
  getTheFile(url)
 

Comments

Popular posts from this blog

Find countries in a file's text using regular expressions and Perl

Make links active in Excel 2007