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

Global name 'parseLog' not defined 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:\Users\Brandon\Downloads\LogCopy1.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 是一个 class。为了访问 python 中 class 的数据成员或方法(除非它是静态的),您首先需要创建 class:[=12= 的实例]

pl = parseLog()

然后更新您的代码:

def main():
    pl = parseLog()
    with open('C:\Users\Brandon\Downloads\LogCopy1.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))