我有一个继承自threading.Thread
. 由于某种原因,线程不想启动。
这是我的代码:
import time,threading,re,socket
class PyWatch(threading.Thread):
filename = ""
def __init__(self,filename):
threading.Thread.__init__(self)
print "initiating..."
self.filename = filename
def run(self):
print "running..."
thefile = open (self.filename)
thefile.seek(0,2) # Go to the end of the file
while True:
line = thefile.readline()
if not line:
time.sleep(0.1) # Sleep briefly
continue
yield line
self.process(line)
def process(self,line):
ip = self.filterIPFromLine(line)
print ip
def filterIPFromLine(self,line):
ip = None
if '/var/ossec/active-response/bin/firewall-drop.sh' in str( line ).lower():
ip = re.match( "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" )
try:
socket.inet_aton(ip[0])
ip = ip[0]
except socket.error:
pass
return ip
tom = PyWatch('example.log')
tom.start()
代码正在运行,它没有返回错误,但由于某种原因它永远不会到达该run()
部分。