2011-08-03 22 views
6

मैं एक फ्लैकी एफ़टीपी सर्वर से फ़ाइलों को डाउनलोड कर रहा हूं जो अक्सर फ़ाइल स्थानांतरण के दौरान बाहर निकलते हैं और मैं सोच रहा था कि डाउनलोड को फिर से कनेक्ट करने और फिर से शुरू करने का कोई तरीका है या नहीं। मैं अजगर के ftplib का उपयोग कर रहा हूँ।टाइमआउट के बाद एफ़टीपी डाउनलोड फिर से शुरू करें

#! /usr/bin/python 

import ftplib 
import os 
import socket 
import sys 

#--------------------------------# 
# Define parameters for ftp site # 
#--------------------------------# 
site   = 'a.really.unstable.server' 
user   = 'anonymous' 
password  = '[email protected]' 
root_ftp_dir = '/directory1/' 
root_local_dir = '/directory2/' 

#--------------------------------------------------------------- 
# Tuple of order numbers to download. Each web request generates 
# an order numbers 
#--------------------------------------------------------------- 
order_num = ('1','2','3','4') 

#----------------------------------------------------------------# 
# Loop through each order. Connect to server on each loop. There # 
# might be a time out for the connection therefore reconnect for # 
# every new ordernumber           # 
#----------------------------------------------------------------# 
# First change local directory 
os.chdir(root_local_dir) 

# Begin loop through 
for order in order_num: 

    print 'Begin Proccessing order number %s' %order 

    # Connect to FTP site 
    try: 
     ftp = ftplib.FTP(host=site, timeout=1200) 
    except (socket.error, socket.gaierror), e: 
     print 'ERROR: Unable to reach "%s"' %site 
     sys.exit() 

    # Login 
    try: 
     ftp.login(user,password) 
    except ftplib.error_perm: 
     print 'ERROR: Unable to login' 
     ftp.quit() 
     sys.exit() 

    # Change remote directory to location of order 
    try: 
     ftp.cwd(root_ftp_dir+order) 
    except ftplib.error_perm: 
     print 'Unable to CD to "%s"' %(root_ftp_dir+order) 
     sys.exit() 

    # Get a list of files 
    try: 
     filelist = ftp.nlst() 
    except ftplib.error_perm: 
     print 'Unable to get file list from "%s"' %order 
     sys.exit() 

    #---------------------------------# 
    # Loop through files and download # 
    #---------------------------------# 
    for each_file in filelist: 

     file_local = open(each_file,'wb') 

     try: 
      ftp.retrbinary('RETR %s' %each_file, file_local.write) 
      file_local.close() 
     except ftplib.error_perm: 
      print 'ERROR: cannot read file "%s"' %each_file 
      os.unlink(each_file) 

    ftp.quit() 

    print 'Finished Proccessing order number %s' %order 

sys.exit() 

त्रुटि है कि मैं: socket.error: [errno 110] कनेक्शन

किसी भी मदद का समय समाप्त हो बहुत सराहना कर रहा है यहाँ कोड है कि मैं का उपयोग कर रहा है।

+0

निश्चित रूप से http://ftputil.sschwarzer.net/trac देखें, यह किसी भी FTP संबंधित कार्य को आसान बना देगा। – agf

उत्तर

3

केवल मानक सुविधाओं (RFC959 देखें) का उपयोग एफ़टीपी के माध्यम से एक डाउनलोड फिर से शुरू करना ब्लॉक संचरण मोड (खंड 3.4.2) है, जो MODE B आदेश का उपयोग कर सेट किया जा सकता के उपयोग की आवश्यकता है। यद्यपि यह सुविधा तकनीकी रूप से विनिर्देश के अनुरूप होने के लिए आवश्यक है, मुझे यकीन नहीं है कि सभी एफ़टीपी सर्वर सॉफ्टवेयर इसे लागू करता है।

ब्लॉक ट्रांसमिशन मोड में, स्ट्रीम ट्रांसमिशन मोड के विपरीत, सर्वर फ़ाइल में भाग भेजता है, जिनमें से प्रत्येक में मार्कर होता है। असफल स्थानांतरण (सेक्शन 3.5) को पुनरारंभ करने के लिए यह मार्कर सर्वर पर पुनः सबमिट किया जा सकता है।

विनिर्देश का कहना है:

[...] a restart procedure is provided to protect users from gross system failures (including failures of a host, an FTP-process, or the underlying network).

हालांकि, AFAIK, विनिर्देश मार्करों के लिए एक आवश्यक जीवन को परिभाषित नहीं करता।

The marker information has meaning only to the sender, but must consist of printable characters in the default or negotiated language of the control connection (ASCII or EBCDIC). The marker could represent a bit-count, a record-count, or any other information by which a system may identify a data checkpoint. The receiver of data, if it implements the restart procedure, would then mark the corresponding position of this marker in the receiving system, and return this information to the user.

यह मान लेना सुरक्षित होना चाहिए कि यह सुविधा लागू करने मार्करों कि एफ़टीपी सत्र के बीच मान्य हैं प्रदान करेगा, लेकिन आपका माइलेज भिन्न हो सकते हैं सर्वर: यह केवल निम्नलिखित कहते हैं।

0

ऐसा करने के लिए, आपको बाधित डाउनलोड रखना होगा, फिर पता लगाएं कि फ़ाइल के कौन से हिस्से आप गायब हैं, उन हिस्सों को डाउनलोड करें और फिर उन्हें एक साथ कनेक्ट करें। मुझे यकीन नहीं है कि यह कैसे करें, लेकिन फ़ायरफ़ॉक्स और क्रोम के लिए एक डाउनलोड मैनेजर है जिसे DownThemAll कहा जाता है जो ऐसा करता है। यद्यपि कोड पायथन में लिखा नहीं गया है (मुझे लगता है कि यह जावास्क्रिप्ट है), आप कोड देख सकते हैं और देख सकते हैं कि यह कैसे करता है।

DownThemll - http://www.downthemall.net/

+0

DownThemAll जावास्क्रिप्ट और एक्सयूएल (एक्सएमएल यूजर इंटरफेस भाषा) में लिखा गया है। स्रोत- http://en.wikipedia.org/wiki/DownThemAll! और https://github.com/nmaier/DownThemAll – Neil