我正在研究一個腳本,它連接「服務器」計算機中的幾臺「客戶端」計算機,然後使用這些客戶端來處理多個文件,使用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()
感謝您的幫助!