19

I am trying to login to this page using Python.

I tried using the steps described on this other Stack Overflow post, and got the following code:

import urllib, urllib2, cookielib

username = 'username'
password = 'password'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})
opener.open('http://friends.cisv.org/index.cfm', login_data)
resp = opener.open('http://friends.cisv.org/index.cfm?fuseaction=activities.list')
print resp.read()

but that gave me the following output:

<SCRIPT LANGUAGE="JavaScript">
    alert('Sorry.  You need to log back in to continue. You will be returned to the home page when you click on OK.');
    document.location.href='index.cfm';
</SCRIPT>

What am I doing wrong?

Community
  • 1
  • 1
iomartin
  • 3,149
  • 9
  • 31
  • 47
  • From the .cfm extension on your handling script, I assume you are using a Coldfusion backend to actually process these logins. We would need to know what the contents and procedure are in that file, because the response is coming into python just fine, it's Coldfusion that's handing back javascript and a redirect instead. This problem is serverside and not in the python client. – DeaconDesperado Nov 29 '11 at 20:16
  • I would assume that the OP does not own the website, and is just hoping to interact with it programmatically as a user. Authenticating to the website is no problem at all, you just need to take a look at what form data is sent when logging in and supply the same data. No need to know what's going on server-side. See my example below. – Acorn Nov 29 '11 at 20:43

2 Answers2

36

I would recommend using the wonderful requests module.

The code below will get you logged into the site and persist the cookies for the duration of the session.

import requests
import sys

EMAIL = ''
PASSWORD = ''

URL = 'http://friends.cisv.org'

def main():
    # Start a session so we can have persistant cookies
    session = requests.session(config={'verbose': sys.stderr})

    # This is the form data that the page sends when logging in
    login_data = {
        'loginemail': EMAIL,
        'loginpswd': PASSWORD,
        'submit': 'login',
    }

    # Authenticate
    r = session.post(URL, data=login_data)

    # Try accessing a page that requires you to be logged in
    r = session.get('http://friends.cisv.org/index.cfm?fuseaction=user.fullprofile')

if __name__ == '__main__':
    main()
Acorn
  • 49,061
  • 27
  • 133
  • 172
  • In the above solution after "r = session.get('http://friends.cisv.org/index.cfm?fuseaction=user.fullprofile')" – Supreeth Meka May 27 '15 at 20:45
  • This solution is not acceptable when you have no control and ability to install external libraries. A native out of the box solution is preferred. – KoCMoHaBTa Jan 09 '16 at 20:44
  • 1
    what is the 'submit': 'login' in here ...I guess it should be the name of submit tag , but in my case there's no name for submit button, it only has ID, so what are the alternatives for submitting with this values. I have replaced 'loginemail' by 'username' and 'loginpswd' by 'password', but please let me know what should be 'submit': 'login' values by looking below code. username: password: Sign In – NikhilP Feb 19 '16 at 05:31
  • 1
    what should be the 'submit'='login' here? is it the name of submit tag or ID or jsp login page name? – NikhilP Feb 19 '16 at 05:37
  • 1
    What does `'submit': 'login'` mean? How did you pick it up? – Blaszard Mar 24 '18 at 10:05
3

The term "login" is unfortunately very vague. The code given here obviously tried to log in using HTTP basic authentication. I'd wager a guess that this site wants you to send it a username and password in some kind of POST form (that's how most web-based login forms work). In this case, you'd need to send the proper POST request, and keep whatever cookies it sent back to you for future requests. Unfortunately I don't know what this would be, it depends on the site. You'll need to figure out how it normally logs a user in and try to follow that pattern.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119