2017-10-22 12 views
1

30 분마다 사진을 찍고 FTP를 통해 내 서버로 보내는 원격 저속 카메라를 만들고 있습니다. 라즈베리 파이는과 같이 파이썬 스크립트를 통해, 카메라를 제어하는 ​​파일을 수집하고 그들을 보내합니다 :FTP 연결이 성공적으로 열렸으며 파일이 파이썬에서 FTP로 업로드되었는지 확인하십시오.

while true 
    captureImages() 
    renameFiles(picID) 
    upload() #uploads any files and folders in the selected path 
    delete() #deletes the uploaded files from the pi 

내 질문 (작동 확인)이 upload 기능과 후속 delete 기능과 관련이있다

  1. 방법은 파일이 부울 'uplo처럼, 성공적으로 업로드되었는지 확인하기 : 여기 무엇이 필요

    def upload():#sends the file to server 
        print ("connecting") 
        #ftp connection (server, user,pass) 
        ftp = ftplib.FTP('server','user','pass') 
    
        #folder from which to upload 
        template_dir = '/home/pi/captures/' 
    
        print ("uploading") 
        #iterate through dirs and files 
        for root, dirs, files in os.walk(template_dir, topdown=True): 
         relative = root[len(template_dir):].lstrip(os.sep) 
         #enters dirs 
         for d in dirs: 
          ftp.mkd(os.path.join(relative, d)) 
         #uploads files 
         for f in files: 
          ftp.cwd(relative) 
          ftp.storbinary('STOR ' + f, open(os.path.join(template_dir, relative, f), 'rb')) 
          ftp.cwd('/') 
    

    두 가지입니다 aded (true/false) '를 사용하여'remove '함수를 트리거하거나 트리거하지 않습니다.

  2. 업로드 프로세스를 건너 뛰는 방법이며 어떤 이유로 든 연결을 설정할 수없는 경우 파일을 삭제하지 마세요. 시간 초과와 마찬가지로 연결을 설정하려고 시도하는 10 초의 창으로, '업로드'와 '제거'를 건너 뛰고 파일을 로컬에 저장하고 while 루프의 다음 반복에서 다시 시도합니다.

도움을 주셔서 감사합니다.

+0

코드 오류가 발생합니다. 'upload'가 실패하면'delete'가 호출되지 않습니다. –

+0

그 문제는 루프가이 경우에 깨질 수 있기 때문에 카메라가 사진 촬영을 중단한다는 것입니다. – mrc

+0

그래서 루프에서 예외를 잡으십시오. –

답변

0

코드 오류가 발생합니다. 따라서 연결이 실패하면 업로드가 수행되지 않습니다. 그리고 upload이 실패하면 delete도 호출되지 않습니다.

당신이해야 할 모든

, 그것은 파괴되지 않도록, 당신의 무한 루프 내에서 예외를 잡을 것입니다 :

while true 
    try: 
     captureImages() 
     renameFiles(picID) 
     upload() #uploads any files and folders in the selected path 
     delete() #deletes the uploaded files from the pi 
    except: 
     print("Error:", sys.exc_info()[0]) 

에 대한 Handling exceptions in Python을 읽어보십시오.

+0

감사합니다. – mrc