Correctly Authorizing in python 3.x

Correctly Authorizing in python 3.x

Postby tsutttra12 » Fri Jan 17, 2014 5:01 pm

Hello,
I have used python at work to optimize different things one of which is bulk image downloading. I ran into some trouble when I needed a bulk image download from a website that needed authorization. I found what looked to be a good resource for python 2.x here: http://www.voidspace.org.uk/python/articles/authentication.shtml in python 3 I replaced to my understanding urllib2 with urllib.request This however doesn't seem to yield much. I have the rest of the script written to loop through an excel spreadsheet, search each column contents, and grab the resulting images through the URL parsed out of the HTML. Is there any good resources to figure out how to authenticate my python program? How will i know if I need to supply cookies or not. The web page I'm trying to get into is http://www.toyotaasg.com and it requires a region/dealer-code authorization instead of a user/pass does the header have to be different because of this?
Last edited by stranac on Fri Jan 17, 2014 5:13 pm, edited 1 time in total.
Reason: First post lock.
tsutttra12
 
Posts: 8
Joined: Fri Jan 17, 2014 4:44 pm

Re: Correctly Authorizing in python 3.x

Postby stranac » Fri Jan 17, 2014 5:16 pm

Basic authentication is not what you need.

You will need to login by sending a post request with your login credentials.
After that, you will have to use the cookies so that the website knows you're logged in.

Doing this using the stdlib is a bit complicated, so I would recommend using requests.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm

Re: Correctly Authorizing in python 3.x

Postby tsutttra12 » Mon Jan 20, 2014 7:41 pm

okay so i'm using the requests module and my code looks something like this
Code: Select all
import requests
auth = {'login_region':'my_region_code', 'login_number':'my_dealer_code'}
with requests.Session() as s:
   s.post('http://toyotaasg.com/login', data=auth)
   r = s.get('http://toyotaasg.com')
   print(r.content)

from my understanding
Code: Select all
requests.Session()
handles the cookies. im compiling this code trying to receive the HTML of the page with authorization and it keeps giving me the HTML for the login screen unauthorized. I've tried different things around changing the
Code: Select all
auth = {'login_region':'my_region_code', 'login_number':'my_dealer_code'}
how does it recognize the authorization form on the page? is it by the input field id, type, name, or something else? Of course if there is something else i'm missing or forgetting I would appreciate any advice
tsutttra12
 
Posts: 8
Joined: Fri Jan 17, 2014 4:44 pm

Re: Correctly Authorizing in python 3.x

Postby stranac » Mon Jan 20, 2014 7:50 pm

You seem to be submitting the wrong parameters.
The parameter names are login[region] and login[number].
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm

Re: Correctly Authorizing in python 3.x

Postby tsutttra12 » Mon Jan 20, 2014 7:54 pm

When I tried that it gives me the same unauthorized page so instead the code was
Code: Select all
import requests
auth = {'login[region]':'my_region_code', 'login[number]':'my_dealer_code'}
with requests.Session() as s:
   s.post('http://toyotaasg.com/login', data=auth)
   r = s.get('http://toyotaasg.com')
   print(r.content)
tsutttra12
 
Posts: 8
Joined: Fri Jan 17, 2014 4:44 pm

Re: Correctly Authorizing in python 3.x

Postby tsutttra12 » Mon Jan 20, 2014 7:59 pm

just got it working
my Get statement didnt have a closing / after the url
tsutttra12
 
Posts: 8
Joined: Fri Jan 17, 2014 4:44 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest