How to download a file from password protected URL in python? - python

from urllib.request import Request, urlopen
import base64
import logging
import urllib
import urllib.request
import requests
def run(file_path,password):
if file_path:
try:
request = Request(file_path)
base64string = base64.b64encode('{}:{}'.format('', password).encode())
#input_file = tempfile.NamedTemporaryFile()
request.add_header("Authorization", "Basic %s" % base64string)
result = urlopen(request)
print (result)
ur = result.geturl()
a = ur.split("/")[:-1]
print (a)
', '.join(a[0:])
url = '/'.join(a)
print (url)
b = ur + "/download"
filename = "myfile"
input_file = requests.get(b,filename)
except Exception as e:
print(e)
l = run("https://cloud.abc.co.uk/s/523aX3O8B5uOWlP","test123")
print (l)
This is the URL https://cloud.abc.co.uk/s/523aX3O8B5uOWlP which is password protected.
I have tried using urllib and base64 for authentication.
This gives mehttps://cloud.abc.co.uk/s/523aX3O8B5uOWlP/authenticate.
This URL redirects to https://cloud.abc.co.uk/s/523aX3O8B5uOWlP/download from where the file can be downloaded.
request = Request(file_path)
base64string = base64.b64encode('{}{}'.format('',password).encode())
request.add_header("Authorization", "Basic %s" % base64string)
result = urlopen(request)
I want to download the file bypassing authentication credentials.

I had a same problem several weeks ago with JSON file. Tried something like this. Hope it works for you as well.
import urllib2
url = "url"
password_mgr = HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, 'username', 'password')
handler = HTTPBasicAuthHandler(password_mgr)
opener = build_opener(handler)
response = opener.open(url)

Related

PYTHON: requests and response 401

I have a little problem with authentication. I am writting a script, which is getting login and password from user(input from keyboard) and then I want to get some data from the website(http not https), but every time I run the script the response is 401.I read some similar posts from stack and I tried this solutions:
Solution 1
c = HTTPConnection("somewebsite")
userAndPass = b64encode(b"username:password").decode("ascii")
headers = { 'Authorization' : 'Basic %s' % userAndPass }
c.request('GET', '/', headers=headers)
res = c.getresponse()
data = res.read()
Solution 2
with requests.Session() as c:
url = 'somewebsite'
USERNAME = 'username'
PASSWORD = 'password'
c.get(url)
login_data = dict(username = USERNAME, password = PASSWORD)
c.post(url,data = login_data)
page = c.get('somewebsite', headers = {"Referer": "somwebsite"})
print(page)
Solution 3
www = 'somewebsite'
value ={'filter':'somefilter'}
data = urllib.parse.urlencode(value)
data=data.encode('utf-8')
req = urllib.request.Request(www,data)
resp = urllib.request.urlopen(req)
respData = resp.read()
print(respData)
x = urllib.request.urlopen(www,"username","password")
print(x.read())'
I don't know how to solve this problem. Can somebody give me some link or tip ?
Have you tried the Basic Authentication example from requests?
>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>
Can I know what type of authentication on the website?
this is an official Basic Auth example (http://docs.python-requests.org/en/master/user/advanced/#http-verbs)
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth('fake#example.com', 'not_a_real_password')
r = requests.post(url=url, data=body, auth=auth)
print(r.status_code)
To use api with authentication, we need to have token_id or app_id that will provide the access for our request. Below is an example how we can formulate the url and get the response:
strong text
import requests
city = input()
api_call = "http://api.openweathermap.org/data/2.5/weather?"
app_id = "892d5406f4811786e2b80a823c78f466"
req_url = api_call + "q=" + city + "&appid=" + app_id
response = requests.get(req_url)
data = response.json()
if (data["cod"] == 200):
hum = data["main"]["humidity"]
print("Humidity is % d " %(hum))
elif data["cod"] != 200:
print("Error occurred : " ,data["cod"], data["message"])

Python - Login to site and post

I will login to site and post to shoutbox.php. I try this :
import urllib
import urllib2
login_data=urllib.urlencode({'username':'daniixxl','password':'steaua','submit':'Login'}) # replace username and password with filed name
op = urllib.urlopen('http://myxz.org/takelogin.php',login_data)
print op.read(100)
url = 'http://myxz.org/shoutbox.php'
data = urllib.urlencode({'shbox_text' : 'joe',
'sent' : 'yes'})
req2 = urllib2.Request(url, login_data)
print data
Problem is: Not post to shoutbox.php
from requests import session
mesaj_postat = {'yupii iar'}
logare = { 'username':'daniixxl',
'password':'steaua'
}
with session() as sesiune:
resp = sesiune.post('http://myxz.org/takelogin.php',data=logare)
if "statusdetails" in resp.text:
print("Logare reusita")
else:
print("Logare nereusita")

Equivalent Python code for the following Java http get requests

I am trying to convert the following Java code to Python. Not sure what I am doing wrong, but I end up with an internal server error 500 with python.
Is the "body" in httplib.httpConnection method equivalent to Java httpentity?
Any other thoughts on what could be wrong?
The input information I collect is correct for sure.
Any help will be really appreciated. I have tried several things, but end up with the same internal server error.
Java Code:
HttpEntity reqEntitiy = new StringEntity("loginTicket="+ticket);
HttpRequestBase request = reMethod.getRequest(uri, reqEntitiy);
request.addHeader("ticket", ticket);
HttpResponse response = httpclient.execute(request);
HttpEntity responseEntity = response.getEntity();
StatusLine responseStatus = response.getStatusLine();
Python code:
url = serverURL + "resources/slmservices/templates/"+templateId+"/options"
#Create the request
ticket = ticket.replace("'",'"')
headers = {"ticket":ticket}
print "ticket",ticket
reqEntity = "loginTicket="+ticket
body = "loginTicket="+ticket
url2 = urlparse.urlparse(serverURL)
h1 = httplib.HTTPConnection(url2.hostname,8580)
print "h1",h1
url3 = urlparse.urlparse(url)
print "url path",url3.path
ubody = {"loginTicket":ticket}
data = urllib.urlencode(ubody)
conn = h1.request("GET",url3.path,data,headers)
#conn = h1.request("GET",url3.path)
response = h1.getresponse()
lines = response.read()
print "response.status",response.status
print "response.reason",response.reason
You don't need to go this low level. Using urllib2 instead:
import urllib2
from urllib import urlencode
url = "{}resources/slmservices/templates/{}/options".format(
serverURL, templateId)
headers = {"ticket": ticket}
params = {"loginTicket": ticket}
url = '{}?{}'.format(url, urlencode(params))
request = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(request)
print 'Status', response.getcode()
print 'Response data', response.read()
Note that the parameters are added to the URL to form URL query parameters.
You can do this simpler still by installing the requests library:
import requests
url = "{}resources/slmservices/templates/{}/options".format(
serverURL, templateId)
headers = {"ticket": ticket}
params = {"loginTicket": ticket}
response = requests.get(url, params=params, headers=headers)
print 'Status', response.status
print 'Response data', response.content # or response.text for Unicode
Here requests takes care of URL-encoding the URL query string parameters and adding it to the URL for you, just like Java does.

Send cookies with urllib2 python

Note: Driver is logged into the website "https://www.example.com"
import urllib,cookielib
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
data_str = ''
all_cookies = driver.get_cookies()
for cookie_name, cookie_value in all_cookies[0].items():
data_str = data_str+cookie_name+'='+str(cookie_value)+';'
opener.addheaders.append( ('Cookie', data_str) )
url = "https://www.example.com/path/"
f = opener.open(url)
print f.read()
This gives me the login page instead of the actual page from url. Can anyone help ?

python urllib2 post request [duplicate]

This question already has answers here:
Is there any way to do HTTP PUT in python
(14 answers)
Closed 9 years ago.
Is it posisble to adapt this piece of code for make put request:
#!/usr/bin/python
import urllib2, base64, urllib
dir="https://domain.com/api/v1/"
use="one#two.com"
pas="123456"
base64string = base64.encodestring('%s:%s' % (use, pas)).replace('\n', '')
request = urllib2.Request(dir, headers={"Authorization" : "Basic %s" % base64string})
response = urllib2.urlopen(request).read()
print response
I try with this other code, but I think that it do a get request, isn't it?
#!/usr/bin/python
import urllib2, base64, urllib
dir="https://domain.com/api/v1/"
use="one#two.com"
pas="123456"
values = {
'list' :["201.22.44.12","8.7.6.0/24"]
}
data = urllib.urlencode(values)
base64string = base64.encodestring('%s:%s' % (use, pas)).replace('\n', '')
request = urllib2.Request(dir, data, headers={"Authorization" : "Basic %s" % base64string})
response = urllib2.urlopen(request).read()
I am not sure It will work or not for you but check this piece of code
You can encode a dict using urllib like this:
import urllib
import urllib2
url = 'http://example.com/...'
values = { 'productslug': 'bar','qty': 'bar' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
print result

Categories