Last active
July 18, 2017 17:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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