Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save file name from response header #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions zonedata-download/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
from urlparse import urlparse
import os
import cgi

# Create a session
s = requests.Session()
Expand Down Expand Up @@ -48,12 +49,15 @@
for url in urls:
r = s.get(config['base_url'] + url)
if r.status_code == 200:
parsed_url = urlparse(r.url)
filename = os.path.basename(parsed_url.path)
_, c = cgi.parse_header(r.headers['content-disposition'])
filename = c['filename']
if filename == "":
parsed_url = urlparse(r.url)
filename = os.path.basename(parsed_url.path) + '.txt.gz'
directory = './zonefiles'
if not os.path.exists(directory):
os.makedirs(directory)
path = directory + '/' + filename + '.txt.gz'
path = directory + '/' + filename
with open(path, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
Expand Down