我有以下代码从"框中读取日志文件"在我们的网络上:
import pycurl
pycurl.Curl()
pycurl_connect = pycurl.Curl()
your_url = 'http://10.0.0.1:1333/cgi/WebCGI?7043'
headers = ['Cookie: loginname=admin; password=e5T%7B%5CYnlcIX%7B; OsVer=2.17.0.32', 'Connection: keep-alive']
pycurl_connect.setopt(pycurl.URL, your_url)
pycurl_connect.setopt(pycurl.HTTPHEADER, headers)
pycurl_connect.perform()
日志文件非常长,我只需要读取未记录在我的数据库中的行。有没有办法逐行获取信息?
感谢。
答案 0 :(得分:0)
我最终使用了非常糟糕的解决方案。我让curl请求将数据保存到文本文件中 - 这是作为守护程序线程启动的 - 而我的程序的其余部分从该文件中读取"逐行"。
必须进行一些体操来复制逐行文件阅读。它最终有点乱,但它确实有效。
以下是代码:
def write_logfile_as_thread(filename):
#Delete content in file
with open(filename, 'w') as file: pass
t = threading.Thread(target=write_logfile, args=(filename,))
t.daemon = True
t.start()
return t
def write_logfile(filename):
storage = open(os.path.join(filename), 'w')
pycurl.Curl()
pycurl_connect = pycurl.Curl()
your_url = 'http://10.0.0.1:1333/cgi/WebCGI?7043'
headers = ['Cookie: loginname=admin; password=e5T%7B%5CYnlcIX%7B; OsVer=2.17.0.32',
'Connection: keep-alive']
pycurl_connect.setopt(pycurl.URL, your_url)
pycurl_connect.setopt(pycurl.HTTPHEADER, headers)
pycurl_connect.setopt(pycurl_connect.WRITEFUNCTION, storage.write)
pycurl_connect.perform()
pycurl_connect.close()
def get_logfile_as_lines(filename):
#file read logic from http://stackoverflow.com/a/3290359/1490584
t = write_logfile_as_thread(filename)
file = open(filename)
text = ""
thread_is_alive = True
while (thread_is_alive):
thread_is_alive = t.is_alive()
where = file.tell()
chunk = file.read()
if not chunk:
file.seek(where)
else:
text += chunk
text_lines = text.split('\n')
if len(text_lines) > 1:
if thread_is_alive:
text = text_lines[-1]
new_lines = text_lines[:-1]
else: #The last line is complete
new_lines = text_lines
for line in new_lines:
yield line
if __name__ == "__main__":
#print only the first 200 lines from the logfile
i = 0
for line in get_logfile_as_lines('logfile.txt'):
if i >= 200:
break
print line
i+=1