Skip to content

Instantly share code, notes, and snippets.

@MattBlissett
Created November 4, 2016 13:55
Show Gist options
  • Save MattBlissett/044ce68c6f06e5209cbb6fb0dc274f86 to your computer and use it in GitHub Desktop.
Save MattBlissett/044ce68c6f06e5209cbb6fb0dc274f86 to your computer and use it in GitHub Desktop.
Set FTP creation and modification times, especially for box.com
# Wraps Python's FTPlib to use implicit FTPS.
#
# Source: http://stackoverflow.com/a/36049814
import ftplib
import ssl
class ImplicitFTP_TLS(ftplib.FTP_TLS):
"""FTP_TLS subclass that automatically wraps sockets in SSL to support implicit FTPS."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._sock = None
@property
def sock(self):
"""Return the socket."""
return self._sock
@sock.setter
def sock(self, value):
"""When modifying the socket, ensure that it is ssl wrapped."""
if value is not None and not isinstance(value, ssl.SSLSocket):
value = self.context.wrap_socket(value)
self._sock = value
#!/usr/bin/python3
# coding=utf8
#
# Set FTP creation and modification times, especially for box.com.
#
import os
from os.path import join, getsize
import datetime
from datetime import datetime
from ImplicitFTP_TLS import ImplicitFTP_TLS
ftp = ImplicitFTP_TLS()
ftp.encoding='utf-8'
ftp.connect(host='ftp.box.com', port=990)
ftp.login(user='xxxxxxxx@example.org', passwd='xxxxxxxx')
ftp.prot_p()
os.chdir('/ftp/Documents')
i = 0
for root, dirs, files in os.walk('.'):
rootnodot = root.lstrip("./") + '/'
print(rootnodot)
for file in files:
path = root+'/'+file
if (not os.path.islink(os.path.join(root, file))):
statinfo = os.lstat(path)
atime = datetime.fromtimestamp(statinfo.st_atime)
mtime = datetime.fromtimestamp(statinfo.st_mtime)
i = i+1
print(i,"\t"+file+" →A "+atime.strftime("%Y%m%d%H%M%S")+" →M "+mtime.strftime("%Y%m%d%H%M%S"))
ftp.sendcmd("MFCT "+atime.strftime("%Y%m%d%H%M%S")+" /Documents/"+rootnodot+file)
ftp.sendcmd("MFMT "+mtime.strftime("%Y%m%d%H%M%S")+" /Documents/"+rootnodot+file)
ftp.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment