Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How do I authenticate a urllib2 script in order to access HTTPS web services from a Django site?
- def login( base_address, username=None, password=None ):
- # prompt for the username (if needed), password
- if username == None:
- username = raw_input( 'Username: ' )
- if password == None:
- password = getpass.getpass( 'Password: ' )
- log.info( 'Logging in %s' % username )
- # fetch the login page in order to get the csrf token
- cookieHandler = urllib2.HTTPCookieProcessor()
- opener = urllib2.build_opener( urllib2.HTTPSHandler(), cookieHandler )
- urllib2.install_opener( opener )
- login_url = base_address + PATH_TO_LOGIN
- log.debug( "login_url: " + login_url )
- login_page = opener.open( login_url )
- # attempt to get the csrf token from the cookie jar
- csrf_cookie = None
- for cookie in cookieHandler.cookiejar:
- if cookie.name == 'csrftoken':
- csrf_cookie = cookie
- break
- if not cookie:
- raise IOError( "No csrf cookie found" )
- log.debug( "found csrf cookie: " + str( csrf_cookie ) )
- log.debug( "csrf_token = %s" % csrf_cookie.value )
- # login using the usr, pwd, and csrf token
- login_data = urllib.urlencode( dict(
- username=username, password=password,
- csrfmiddlewaretoken=csrf_cookie.value ) )
- log.debug( "login_data: %s" % login_data )
- req = urllib2.Request( login_url, login_data )
- response = urllib2.urlopen( req )
- # <--- 403: FORBIDDEN here
- log.debug( 'response url:n' + str( response.geturl() ) + 'n' )
- log.debug( 'response info:n' + str( response.info() ) + 'n' )
- # should redirect to the welcome page here, if back at log in - refused
- if response.geturl() == login_url:
- raise IOError( 'Authentication refused' )
- log.info( 't%s is logged in' % username )
- # save the cookies/opener for further actions
- return opener
- import urllib
- import urllib2
- import contextlib
- def login(login_url, username, password):
- """
- Login to site
- """
- cookies = urllib2.HTTPCookieProcessor()
- opener = urllib2.build_opener(cookies)
- urllib2.install_opener(opener)
- opener.open(login_url)
- try:
- token = [x.value for x in cookies.cookiejar if x.name == 'csrftoken'][0]
- except IndexError:
- return False, "no csrftoken"
- params = dict(username=username, password=password,
- this_is_the_login_form=True,
- csrfmiddlewaretoken=token,
- )
- encoded_params = urllib.urlencode(params)
- with contextlib.closing(opener.open(login_url, encoded_params)) as f:
- html = f.read()
- print html
- # we're in.
- req.add_header( 'Referer', login_url )
Add Comment
Please, Sign In to add comment