Commit a38c6aeb authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(proxy) add ftp support again for remote acl download. We switched to python's...

(proxy) add ftp support again for remote acl download. We switched to python's requests library, which doesn't support ftp. Some people still use ftp though.

(cherry picked from commit c3e84685)
parent df45ff8a
...@@ -37,6 +37,7 @@ import gzip ...@@ -37,6 +37,7 @@ import gzip
import zipfile import zipfile
import syslog import syslog
import shutil import shutil
import urllib2
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
import requests import requests
...@@ -54,7 +55,7 @@ class Downloader(object): ...@@ -54,7 +55,7 @@ class Downloader(object):
:param url: source url :param url: source url
:param timeout: timeout in seconds :param timeout: timeout in seconds
""" """
self._url = url self._url = url.strip()
self._timeout = timeout self._timeout = timeout
self._source_handle = None self._source_handle = None
self._username = username self._username = username
...@@ -63,18 +64,38 @@ class Downloader(object): ...@@ -63,18 +64,38 @@ class Downloader(object):
def fetch(self): def fetch(self):
""" fetch (raw) source data into tempfile using self._source_handle """ fetch (raw) source data into tempfile using self._source_handle
""" """
if self._username is not None: self._source_handle = None
req = requests.get(url=self._url, stream=True, timeout=self._timeout, auth=(self._username, self._password)) if self._url.lower().startswith('http://') or self._url.lower().startswith('https://'):
else: # HTTP(S) download
req = requests.get(url=self._url, stream=True, timeout=self._timeout) if self._username is not None:
if req.status_code == 200: req = requests.get(url=self._url, stream=True, timeout=self._timeout,
self._source_handle = tempfile.NamedTemporaryFile() auth=(self._username, self._password))
shutil.copyfileobj(req.raw, self._source_handle) else:
self._source_handle.seek(0) req = requests.get(url=self._url, stream=True, timeout=self._timeout)
if req.status_code == 200:
self._source_handle = tempfile.NamedTemporaryFile()
shutil.copyfileobj(req.raw, self._source_handle)
self._source_handle.seek(0)
else:
syslog.syslog(syslog.LOG_ERR, 'proxy acl: error downloading %s (http code: %s)' % (self._url,
req.status_code))
elif self._url.lower().startswith('ftp://'):
# FTP download
try:
f = urllib2.urlopen(self._url, timeout=self._timeout)
self._source_handle = tempfile.NamedTemporaryFile()
while True:
data = f.read(1024)
if not data:
break
else:
self._source_handle.write(data)
self._source_handle.seek(0)
f.close()
except (urllib2.URLError, urllib2.HTTPError, IOError) as e:
syslog.syslog(syslog.LOG_ERR, 'proxy acl: error downloading %s' % self._url)
else: else:
syslog.syslog(syslog.LOG_ERR, 'proxy acl: error downloading %s (http code: %s)' % (self._url, syslog.syslog(syslog.LOG_ERR, 'proxy acl: unsupported protocol for %s' % self._url)
req.status_code))
self._source_handle = None
def get_files(self): def get_files(self):
""" process downloaded data, handle compression """ process downloaded data, handle compression
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment