Skip to content

Instantly share code, notes, and snippets.

@lanfon72
Created August 4, 2017 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanfon72/1aa957f992b4ec337cdd920cb8151d72 to your computer and use it in GitHub Desktop.
Save lanfon72/1aa957f992b4ec337cdd920cb8151d72 to your computer and use it in GitHub Desktop.
saving files from garner ftp with requests.
import os
from ftplib import FTP
import requests
def get_connect(url):
root, foldr = url.split('//', 1)[-1].split('/', 1)
ftp = FTP(root)
ftp.login()
ftp.cwd(foldr)
return ftp
def save_file(path, url):
try:
data = requests.get(url).content
with open(path, 'wb') as f:
f.write(data)
except Exception as e:
print("get error on", path, "due to", repr(e))
return path
else:
print(path, "saved.")
return None
def main(files, url):
fname = files.pop()
while files:
fname = save_file(fname, "%s/%s" % (url, fname))
fname = fname if fname else files.pop()
if __name__ == '__main__':
url = "garner.ucsd.edu/pub/rinex/2003/303"
conn = get_connect(url)
files = set(conn.nlst()).difference(os.listdir())
conn.quit()
if files:
url = r'http://anonymous:jason%40ucsd.edu@' + url
main(files, url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment