Skip to content

Instantly share code, notes, and snippets.

@Andimeo
Last active July 18, 2017 17:06
Show Gist options
  • Save Andimeo/db9f8afc055226c1a319fe1139c33c7f to your computer and use it in GitHub Desktop.
Save Andimeo/db9f8afc055226c1a319fe1139c33c7f to your computer and use it in GitHub Desktop.
Use python's SimpleHTTPServer to transfer documents in the same LAN in a batch manner
import argparse, os, pprint, re, shutil, urllib.request, urllib.parse
parser = argparse.ArgumentParser()
parser.add_argument('link')
parser.add_argument('pos')
args = parser.parse_args()
if not os.path.exists(args.pos):
os.mkdir(args.pos)
with urllib.request.urlopen(args.link) as fp:
html = fp.read().decode('utf-8')
pattern = '<a href="(.*)">'
file_names = re.findall(pattern, html)
unquoted_file_names = [urllib.parse.unquote(f) for f in re.findall(pattern, html)]
for i in range(len(file_names)):
remote_path = urllib.request.urljoin(args.link, file_names[i])
local_path = os.path.join(args.pos, unquoted_file_names[i])
with urllib.request.urlopen(remote_path) as rp, open(local_path, 'wb') as lp:
shutil.copyfileobj(rp, lp)
print('Succeeded downloading: %s' % unquoted_file_names[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment