未定义的全局名称'parseLog' Python 2.7



我已经通读了类似的主题,但我仍然不确定自己做错了什么。我收到错误,未定义全局名称"parseLog"。此外,我一直在摸索如何在这里正确地格式化代码,但在我的编辑器中,所有内容都正确地缩进了,只是对在这里这样做感到恼火。

import urllib2
import re
import json
class parseLog:
    def __init__(self):
        ''' Get access log location and pattern(Common Log Format)'''
    self.apacheLog = 'https://raw.githubusercontent.com/access.log'
    self.data = urllib2.urlopen(self.apacheLog)
    self.parts = [
    r'(?P<host>S+)', 
    r'S+', 
    r'(?P<user>S+)', 
    r'[(?P<time>.+)]',
    r'"(?P<request>.+)"', 
    r'(?P<status>[0-9]+)', 
    r'(?P<size>S+)', 
    r'"(?P<referer>.*)"', 
    r'"(?P<agent>.*)"', 
    ]
    self.pat = re.compile(r's+'.join(self.parts)+r's*Z')
def rescheck(result):
    '''Check for absent refer page'''
    if result["referer"] == "-":
        result["referer"] = None
    return
def getIPinfo(IP,result):
    '''Get lat,long,org,and ISP using IP address from log record'''
    apiURL = 'http://ip-api.com/json/'+IP            
    getResp = urllib2.urlopen(apiURL)
    jsonResp = json.load(getResp)
    result["lat"] = jsonResp['lat']
    result["long"] = jsonResp['lon']
    result["org"] = jsonResp['org']
    result["ISPname"] = jsonResp['isp']
    finalKeys = ["time","request","referer","host","org","lat","long","ISPname"]
    finalDict = {x: result[x] for x in finalKeys if x in result}
    print finalDict
    return finalDict
def main():
    with open('C:UsersBrandonDownloadsLogCopy1.txt','a') as f:
        for line in parseLog.data:
            match = parseLog.pat.match(line)
            result = match.groupdict()
            theIP = result["host"]
            print theIP
            parseLog.rescheck(result)
            dictToWrite = parseLog.getIPinfo(theIP,result)
            f.write(str(dictToWrite))
            #print str(dictToWrite)
main()

parseLog是一个类。为了访问python中类的数据成员或方法(除非是静态的),您首先需要创建该类的实例:

pl = parseLog()

然后更新您的代码:

def main():
    pl = parseLog()
    with open('C:UsersBrandonDownloadsLogCopy1.txt','a') as f:
        for line in pl.data:
            match = pl.pat.match(line)
            result = match.groupdict()
            theIP = result["host"]
            print theIP
            rescheck(result)
            dictToWrite = getIPinfo(theIP,result)
            f.write(str(dictToWrite))

相关内容

  • 没有找到相关文章

最新更新