Pyogp Client Lib/Login Test Script

From Second Life Wiki
Jump to navigation Jump to search

Eventlet httpc version

import os.path
if os.path.exists("../setup_path.py"):
	execfile("../setup_path.py")

import time
from indra.base import llsd
from eventlet import api, httpc, util

util.wrap_socket_with_coroutine_socket()

loginparams={'password'  : 'pass',   # md5-password '$1$' + md5hash
            'lastname'  : 'last',
            'firstname' : 'first',
}

llsdloginparams = llsd.format_xml(loginparams)
headers = {"content-type" : "application/llsd+xml"}

try:
    response = httpc.post('https://login1.aditi.lindenlab.com/cgi-bin/auth.cgi', llsdloginparams, headers)
    loginurl = response.read()
except httpc.Found, e:
    loginurl = e.location()

print loginurl

data = {'caps': {'place_avatar' : True}}
data = llsd.format_xml(data)
headers = {"content-type" : "application/llsd+xml"}

response = httpc.post(loginurl, data, headers)
request = response.read()

print request


Urllib2 version


import os.path
if os.path.exists("../setup_path.py"):
	execfile("../setup_path.py")

import time
from indra.base import llsd
import urllib2
from eventlet import util

util.wrap_socket_with_coroutine_socket()

loginparams={'password'  : 'pass',   # md5-password '$1$' + md5hash
            'lastname'  : 'last',
            'firstname' : 'first',
}

llsdloginparams = llsd.format_xml(loginparams)
headers = {"content-type" : "application/llsd+xml"}

request = urllib2.Request('https://login1.aditi.lindenlab.com/cgi-bin/auth.cgi', llsdloginparams, headers)

# todo: handle non 302 cases! there's plenty, and lots are test cases
# per the protocol, a successful authentication returns a 302 with the seedcap embedded in the headers 
class RedirectHandler(urllib2.HTTPRedirectHandler):

    def http_error_302(self, req, fp, code, msg, headers):
        #parse the redirect, grabbing the seed cap url from the headers
        if True:
            print ''
            print 'contents of response = '
            print headers
            print msg
            print ''
        

        # per the protocol, the seedcap is in the 'location' in headers
        return headers['location']

# post to auth.cgi, ignoring the built in redirect

opener = urllib2.build_opener(RedirectHandler())

try:
    seed_cap = opener.open(request)
except urllib2.HTTPError, e:
    print 'HTTP Error: ', e.code
except urllib2.URLError, e:
    print 'URL Error: ', e.reason

print seed_cap

data = {'caps': {'place_avatar' : True, 'event_queue' : True}}
data = llsd.format_xml(data)
headers = {"content-type" : "application/llsd+xml"}

request = urllib2.Request(seed_cap, data, headers)
response = urllib2.urlopen(request)
request = response.read()

print request