我正在研究一个脚本,它连接“服务器”计算机中的几台“客户端”计算机,然后使用这些客户端来处理多个文件,使用FTP(pyftplib和pyftpdlib )用于传输文件和结果。构建python FTP脚本中的错误检查功能
该脚本的工作原理是在服务器上创建3个文件夹:文件,处理和结果。然后,客户端通过FTP连接到服务器,访问“文件”文件夹,获取文件进行处理,然后在处理文件时将其传输到“处理”文件夹。然后,当它完成处理时,客户端从处理文件夹中删除文件并将结果复制到“结果”文件夹中。
这是工作正常,无论是在服务器端和客户端。我遇到的问题是,如果其中一个客户端中途断开连接而没有产生错误(PC断开连接,断电),服务器将威胁到这一点,就好像客户端仍在处理该文件一样,该文件将保持在“处理”文件夹。我想要的是一个错误检查功能,当发生这种情况时,“Processing”文件夹中的文件将返回到“Files”文件夹。
这里是服务器FTP规则
def main():
authorizer = DummyAuthorizer()
authorizer.add_user('client', 'password', '.', perm='elradfmwM')
authorizer.add_anonymous(os.getcwd())
handler = FTPHandler
handler.authorizer = authorizer
handler.banner = "FTP Server."
address = ('', port)
server = FTPServer(address, handler)
server.max_cons = 256
server.max_cons_per_ip = 50
server.serve_forever()
if __name__ == '__main__':
main()
这里是FTP客户端代码:
while True:
ftp = ftplib.FTP()
ftp.connect(arguments.host_ip, arguments.host_port)
ftp.login("client", "password")
print ftp.getwelcome()
ftp.retrlines('LIST')
ftp.retrbinary('RETR Output.txt', open('Output.txt', 'wb').write)
ftp.retrbinary('RETR dicionario.json', open('dicionario.json', 'wb').write)
with open('dicionario.json') as json_file:
json_data = json.load(json_file)
receptor_file = json_data['--receptor']
print 'Retrieving receptor file ' + receptor_file
ftp.retrbinary('RETR ' + receptor_file, open(receptor_file, 'wb').write)
ftp.cwd('Files')
ftp.retrlines('LIST')
filename = ftp.nlst()[0]
print 'Getting ' + filename
ftp.retrbinary('RETR ' + filename, open(filename, 'wb').write)
with open("Output.txt", "a") as input_file:
input_file.write('ligand = %s' %filename)
input_file.close()
ftp.delete(filename)
ftp.cwd('../Processing')
ftp.storbinary('STOR ' + filename, open(filename, 'rb'))
ftp.quit()
print "Processing"
return_code = subprocess.call(calls the program for processing files)
if return_code == 0:
print """Done!"""
ftp.connect(arguments.host_ip, arguments.host_port)
ftp.login("client", "password")
ftp.cwd('Results')
ftp.storbinary('STOR ' + os.path.splitext(filename)[0] + '_out.pdbqt', open (os.path.splitext(filename)[0] + '_out.pdbqt'))
ftp.cwd('../Processing')
ftp.delete(filename)
ftp.quit()
else:
print """Something is technically wrong..."""
ftp.connect(arguments.host_ip, arguments.host_port)
ftp.login("client", "password")
ftp.cwd('Files')
ftp.storbinary('STOR ' + filename, open(filename, 'rb'))
ftp.cwd('../Processing')
ftp.delete(filename)
ftp.quit()
感谢您的帮助!