Apr-22-2024, 09:28 AM
Looking at using yield, I have this simulated log file: logsim.py
logsim.py has 2 very long lists, ips and docs
ips like this: ips = ['129.194.8.73', '198.37.27.153', '217.127.12.71', ...
docs like this: docs = ['"GET /ply/ply-2.0.tar.gz HTTP/1.0" 304 -',
'"GET /python/tutorial/beazley_advanced_python/Slides/SLIDE066.HTM HTTP/1.0" 200 1538', ....
The tutorial is here.
logsim.py code, apart from the lists ips and docs:
But nothing is written to /home/pedro/temp/access-log.log when I run this in bash
Should I change f.flush() for f.write(ips[n] + ',' + date + ',' + docs[m])
Often, the file does not change until f.close() is called, but I can't do that, because logsim.py is still writing to f!
How to get the data actually written to /home/pedro/temp/access-log.log without closing the file?
How to dynamically update the file, without closing it?
logsim.py has 2 very long lists, ips and docs
ips like this: ips = ['129.194.8.73', '198.37.27.153', '217.127.12.71', ...
docs like this: docs = ['"GET /ply/ply-2.0.tar.gz HTTP/1.0" 304 -',
'"GET /python/tutorial/beazley_advanced_python/Slides/SLIDE066.HTM HTTP/1.0" 200 1538', ....
The tutorial is here.
logsim.py code, apart from the lists ips and docs:
import time, random # a fake log programme f = open("/home/pedro/temp/access-log.log","w") while True: time.sleep(random.random()) n = random.randint(0,len(ips)-1) m = random.randint(0,len(docs)-1) t = time.time() date = time.strftime("[%d/%b/%Y:%H:%M:%S -0600]",time.localtime(t)) #print(f,"%s - - %s %s" % (ips[n],date,docs[m])) print(f'{f} {ips[n]},{date},{docs[m]}') f.flush()For the tutorial it says, leave this running in the background, so I run it in bash.
But nothing is written to /home/pedro/temp/access-log.log when I run this in bash
Should I change f.flush() for f.write(ips[n] + ',' + date + ',' + docs[m])
Often, the file does not change until f.close() is called, but I can't do that, because logsim.py is still writing to f!
How to get the data actually written to /home/pedro/temp/access-log.log without closing the file?
How to dynamically update the file, without closing it?