| Current File : //usr/ddu/scripts/url_down.py |
#!/usr/bin/python2.7
#
# Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
#
"""
Download file from http/ftp server
"""
import urllib2
import sys
import os
import re
import ftplib
def checkurl(url):
"""
check if url is valid
"""
url_file = os.path.basename(url).replace(" ", "").replace("\t", "")
url_files = url_file.split(",")
url_protocol = os.path.dirname(url)
for file_down in url_files:
filename = os.path.join(url_protocol, file_down)
try:
urllib2.urlopen(filename)
except IOError as e:
print "Error found when downloading %s: %s" \
% (filename, e.strerror)
return 1
return 0
def downloadurl(url):
"""
download file from http/ftp server,
the url should not include space/comma characters.
"""
url_file = os.path.basename(url).replace(" ", "").replace("\t", "")
url_files = url_file.split(",")
url_protocol = os.path.dirname(url)
if re.compile('^http://').search(url_protocol):
for file_down in url_files:
try:
filename = os.path.join(url_protocol, file_down)
tempfile = os.path.join('/tmp', file_down)
net_content = urllib2.urlopen(filename)
doc_content = net_content.read()
local_file = open(tempfile, "w+")
local_file.write(doc_content)
local_file.close()
net_content.close()
print tempfile
except IOError as e:
print "Error found when downloading %s: %s" \
% (filename, e.strerror)
return 1
elif re.compile('^ftp://').search(url_protocol):
user_pass = url_protocol.split('@')[0].split('//')[1]
check_user = len(re.compile(':').findall(user_pass))
# ftp url usually need to provide user name and password for
# the ftp server.
if check_user == 0:
# if user name password have not been provided, then use default
# user name anonymous and password test@test.mail
ftp_user = 'anonymous'
ftp_pass = 'test@test.mail'
ftp_server = url_protocol.split('//')[1].split('/')[0]
elif check_user == 1:
# if user name password are not empty, use them.
ftp_user, ftp_pass = user_pass.split(':')
ftp_server = url_protocol.split('@')[1].split('/')[0]
else:
# Error: more than one user name or password
# was given when checking ftp authority.
print "Error: more than one user name or password \
was given when checking ftp authority"
return 1
for file_down in url_files:
try:
filename = os.path.join(
'/',
os.path.dirname(
url.split('@')[1].split(
'/',
1)[1]),
file_down)
tempfile = os.path.join('/tmp', file_down)
ftp = ftplib.FTP(ftp_server)
ftp.login(ftp_user, ftp_pass)
ftp.retrbinary('RETR ' + filename, open(tempfile, "wb").write)
print tempfile
except IOError as e:
print "Error found when downloading %s: %s" \
% (filename, e.strerror)
return 1
return 0
if __name__ == '__main__':
if sys.argv[1] == "checkurl":
RESULT = checkurl(sys.argv[2])
elif sys.argv[1] == "download":
RESULT = downloadurl(sys.argv[2])
if RESULT != 0:
sys.exit(1)