Skip to content

Instantly share code, notes, and snippets.

@paultopia
Last active February 7, 2016 21:24
Show Gist options
  • Save paultopia/8462460b9497f6c6de5d to your computer and use it in GitHub Desktop.
Save paultopia/8462460b9497f6c6de5d to your computer and use it in GitHub Desktop.
Hacktastic quick ftp upload (for one-line web deployment etc.)
""" see http://stackoverflow.com/questions/35245929/python-ftplib-hangs-on-upload-stor-and-not-network-latency for the ftplib hell that led here
Obvs replace [SERVER] [USER] and [PASSWORD] with appropriate values, and add to the list of ascii extensions for whatever you use.
"""
import sys
import os
import datetime as dt
full = sys.argv[1]
path = 'public_html/' + full[:full.rfind('/') + 1]
filename = full[full.rfind('/') + 1:]
def picktype(filename):
if filename.endswith(('.htm','.html','.py','.cgi','.js','.css','.txt','.md', '.svg')):
return 'ascii'
return 'binary'
ftpstring = """
open [SERVER]
user [USER] [PASSWORD]
cd {0}
{1}
put {2}
bye
""".format(path, picktype(filename), filename)
print ftpstring
tempfilename = 'tempfile{:%Y%m%d%H%M%S}.txt'.format(dt.datetime.now())
with open(tempfilename, 'w') as tempfile:
tempfile.write(ftpstring)
commandstring = 'ftp -n < {}'.format(tempfilename)
os.system(commandstring)
os.remove(tempfilename)
print 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment