Reputation: 143
I have a script that calls functions from a daemon server. The daemon server generates an output each time it finishes its job. The daemon mainly downloads files from an outside server. The daemon source code is closed (coded by an external company).
Daemon Output Example after it finishs downloading the file:
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf
The script i'm using gets ids of the downloads from a TXT file, Username of the PC for the From User Field , and put them in an array then call them from the Daemon get the file then write a log in a txt file.
The Log Sample should be:
Documnent ID= 3
From User = DomainCon
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf
the problem I'm facing is that Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf which the daemon server generates everytime a download finishes is sometimes missing in the TXT.
Problem Example:
Documnent ID= 2
From User = DomainCon
Documnent ID= 3
In Chat = DomainCon
ANSWER 78
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf
The output in the log file should be:
Documnent ID= 2
From User = DomainCon
ANSWER 96
Saved to /home/ubuntu/Daemon/downloads/download_1024000002.pdf
Documnent ID= 3
In Chat = DomainCon
ANSWER 78
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf
this is the Script I'm using including comments:
for i in range(len(ids)):\\ ids is the array that contains the documents that should be downloaded.
cmdping = "sleep 5; echo load_document "+ids[i][0]+"| nc -w 4 127.0.0.1 1234 | tee >> "+logtxt \\ Perpare the Echo command to the daemon to start the download tee to save the output of the daemon into the log text
print("\033[1;31m~~~Documnent~~~NB~~~"+str(i+1)+"\033[1;m")
print ("Documnent ID= "+ids[i][0])\\ Just Print on screen for Debbuging Purpose
print ("From User= "+ids[i][1])\\Just Print on screen for Debbuging Purpose
logfile = open(logtxt, "a") \\ Open the TXT where the log is gonna be saved.
logfile.write("Documnent ID= "+ids[i][0]+"\n")\\Write the Document ID in the File.
logfile.write("From User = "+ids[i][1]+"\n")\\\\Write the From User in the File.
logfile.close()\\Close the logfile
if (i==len(ids)-1):
p=subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)\\ Start the proccess that was prepared before.
#time.sleep(1)
check=""
s=1
skip=0
while (s==1):
check=checkfile(srcdir)
if (check!="no"):
s=2
if(check=="skip"):
skip=1
if(skip!=1):
if(checksize(srcdir+check) == "done"):
print(check+"\n \033[1;32mFinished Downloading moving to the next download\033[1;m")
p.terminate()
user_dir=rootdir+"/document/"+ids[i][1];
checkandcopy(download,user_dir)
Upvotes: 3
Views: 112
Reputation: 7110
You could consider using a socket instead. Here's some inspiration:
import re
import socket
for i, (doc_id, user) in enumerate(ids):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("\033[1;31m~~~Documnent~~~NB~~~"+str(i+1)+"\033[1;m")
s.connect(('127.0.0.1', 1234))
s.sendall('load_document {}\n'.format(doc_id))
buf = []
while True:
r = s.recv(4096).strip()
print('Got {} for socket'.format(r))
buf.append(r)
if r.endswith('.pdf'):
print('Done')
break
result = '\n'.join(buf)
s.close()
with open(logtxt, 'a') as f:
f.write('Document ID= {}\n'.format(doc_id))
f.write('From User = {}\n'.format(user))
f.write('{}\n'.format(result))
filename = re.search('Saved to (.+)$', result).group(1)
checkandcopy(filename, userdir)
Upvotes: 1