守护程序输出未写入TXT

2024-05-29 03:21:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从守护服务器调用函数的脚本。守护进程服务器每次完成其作业时都会生成一个输出。守护进程主要从外部服务器下载文件。守护程序源代码已关闭(由外部公司编码)。你知道吗

完成文件下载后的守护程序输出示例:

Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

我使用的脚本从一个TXT文件中获取下载的id,from User字段的PC用户名,并将它们放入一个数组中,然后从守护进程中调用它们获取文件,然后在一个TXT文件中写入日志。你知道吗

日志样本应为:

Documnent ID= 3
From User = DomainCon
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

我面临的问题是保存到/home/ubuntu/Daemon/downloads/download\u 1024000003.pdf中的文件有时会在TXT文件中丢失,每次下载完成后,Daemon服务器都会生成这个文件。你知道吗

问题示例:

Documnent ID= 2
From User = DomainCon
Documnent ID= 3
In Chat = DomainCon
ANSWER 78
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

日志文件中的输出应为:

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

这是我正在使用的脚本,包括注释:

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)

Tags: 文件thetofromididshomepdf
1条回答
网友
1楼 · 发布于 2024-05-29 03:21:45

你可以考虑改用插座。以下是一些灵感:

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)

相关问题 更多 >

    热门问题