Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file from https with authentication

I have a Python 2.6 script that downloades a file from a web server. I want this this script to pass a username and password(for authenrication before fetching the file) and I am passing them as part of the url as follows:

import urllib2
response = urllib2.urlopen("http://'user1':'password'@server_name/file")

However, I am getting syntax error in this case. Is this the correct way to go about it? I am pretty new to Python and coding in general. Can anybody help me out? Thanks!

like image 981
user3262537 Avatar asked Feb 13 '16 04:02

user3262537


2 Answers

If you can use the requests library, it's insanely easy. I'd highly recommend using it if possible:

import requests

url = 'http://somewebsite.org'
user, password = 'bob', 'I love cats'
resp = requests.get(url, auth=(user, password))
like image 148
willnx Avatar answered Sep 20 '22 13:09

willnx


I suppose you are trying to pass through a Basic Authentication. In this case, you can handle it this way:

import urllib2

username = 'user1'
password = '123456'

#This should be the base url you wanted to access.
baseurl = 'http://server_name.com'

#Create a password manager
manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, baseurl, username, password)

#Create an authentication handler using the password manager
auth = urllib2.HTTPBasicAuthHandler(manager)

#Create an opener that will replace the default urlopen method on further calls
opener = urllib2.build_opener(auth)
urllib2.install_opener(opener)

#Here you should access the full url you wanted to open
response = urllib2.urlopen(baseurl + "/file")
like image 23
Arton Dorneles Avatar answered Sep 20 '22 13:09

Arton Dorneles