summaryrefslogtreecommitdiff
path: root/ftp.py
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-07-13 12:36:10 +0200
committerPeter Wu <lekensteyn@gmail.com>2013-07-13 12:36:10 +0200
commita9ae058b06ef0236fc64237e5c6dc7b779e13468 (patch)
treeff91991e89b0bb18f37cfc2e8beeb9fb29c2079a /ftp.py
parenta4471f71aad10433725161dff9ac39b8f23c4d77 (diff)
downloadscripts-a9ae058b06ef0236fc64237e5c6dc7b779e13468.tar.gz
ftp*: python3 compat, SIZE fixes, MLSD support
Set binary mode before requesting size, do not exit program if size() fails. Support MLSD mode for ftp-list.py (mostly copied from ftp.py)
Diffstat (limited to 'ftp.py')
-rwxr-xr-xftp.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/ftp.py b/ftp.py
index b562d20..b2c4a3d 100755
--- a/ftp.py
+++ b/ftp.py
@@ -3,6 +3,9 @@
#
# Copyright (C) 2013 Peter Wu <lekensteyn@gmail.com>
+from __future__ import print_function, division
+# TODO: nonlocal is python3-only which makes the above useless
+
import sys
from ftplib import FTP, all_errors
import re
@@ -169,7 +172,12 @@ def download_file(ftp, file_path, local_file_path, size=None, offset=0):
os.makedirs(local_dirs, exist_ok=True)
if size is None:
- size = ftp.size(file_path)
+ try:
+ # ProFTPd requires binary mode for size
+ ftp.voidcmd("TYPE I")
+ size = ftp.size(file_path)
+ except all_errors as e:
+ print("SIZE failed: ", e, file=sys.stderr)
BAR_WIDTH = 50
if size is not None:
@@ -248,7 +256,12 @@ def download_file(ftp, file_path, local_file_path, size=None, offset=0):
def reget_file(ftp, file_path, local_file_path):
# do not send a REST(art) command when starting at the beginning
offset = None
- file_size = ftp.size(file_path)
+ try:
+ ftp.voidcmd("TYPE I")
+ file_size = ftp.size(file_path)
+ except all_errors as e:
+ print("SIZE failed: ", e, file=sys.stderr)
+ file_size = None
if file_size is not None:
try:
local_size = os.path.getsize(local_file_path)