← Back to team overview

dhis2-users team mailing list archive

Re: [Dhis2-devs] Automating DHIS2

 

Use at your own risk :-)  Really just me figuring out how to do
cookies with python.

On 7 November 2010 11:21, Jason Pickering <jason.p.pickering@xxxxxxxxx> wrote:
> Hi Bob,
> Would be good to see what you did with python. One man's trash it
> another's treasure. :)
>
> Selenium seems a bit overkill perhaps. A simple script called from the
> command line with cron/task scheduler would probably be more enough
> for me at this point, if I could figure out how to authenticate
> easily.
>
> Regards,,
> Jason
>
>
> On Sun, Nov 7, 2010 at 12:06 PM, Knut Staring <knutst@xxxxxxxxx> wrote:
>> On Sun, Nov 7, 2010 at 10:53 AM, Bob Jolliffe <bobjolliffe@xxxxxxxxx> wrote:
>>
>>> I have worked around basic auth using python scripts and maintaining
>>> state with cookies like the browser does, but it's a bit awkward and
>>> I'm not sure I'd recommend it.
>>>
>>> I'll try and rummage back to find that (thrown away?) code.
>>
>> This guy seems to have been doing sth similar:
>> http://solobonite.wikidot.com/seleniumrunner-tutorial
>>
>
>
>
> --
> Jason P. Pickering
> email: jason.p.pickering@xxxxxxxxx
> tel:+260968395190
>
import sys
import urllib, urllib2, cookielib

DHIS_URL="http://localhost:8082";

DHIS_USER="admin"
DHIS_PASS="district"

DHIS_LOGIN="/dhis-web-commons/security/login.jsp"
DHIS_LOGIN_ACTION="/dhis-web-commons-security/login.action"

try:
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

    params = urllib.urlencode({'j_username': DHIS_USER, 'j_password': DHIS_PASS})
    r = opener.open(DHIS_URL+DHIS_LOGIN_ACTION, params)

    # should check we have the session cookie now ... hoping for the best	
    r = opener.open(DHIS_URL + "/dhis-web-importexport/displayImportForm.action")
    print r.read()
    # voila
	    
    sys.exit(0)
    
except Exception as ex:
    print "ouch: ",ex
    sys.exit(1)

References