This does patch updates Gramps to the 3.2 syntax, it does not yet mean Gramps works with python 3.2 Expect next day commits to fix further issues, but this is the main 2to3 tool created patch changed where needed to have python 2.7 work. Specific issues might be: 1. next has been changed, must be checked 2. new division as on the wiki page listed is to do 3. ... svn: r20634
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
import sys
|
|
if sys.version_info[0] < 3:
|
|
from urllib2 import (urlopen, Request, HTTPCookieProcessor,
|
|
build_opener, install_opener)
|
|
from urllib import urlencode
|
|
else:
|
|
from urllib.request import (Request, urlopen, HTTPCookieProcessor,
|
|
build_opener, install_opener)
|
|
from urllib.parse import urlencode
|
|
|
|
class Connection(object):
|
|
"""
|
|
>>> conn = Connection()
|
|
>>> response = conn.login("http://blankfamily.us/login/", "username", "password")
|
|
"""
|
|
def login(self, login_url, username, password):
|
|
cookies = HTTPCookieProcessor()
|
|
opener = build_opener(cookies)
|
|
install_opener(opener)
|
|
opener.open(login_url)
|
|
try:
|
|
self.token = [x.value for x in cookies.cookiejar if x.name == 'csrftoken'][0]
|
|
except IndexError:
|
|
return Exception("no csrftoken")
|
|
params = dict(username=username,
|
|
password=password,
|
|
next="/",
|
|
csrfmiddlewaretoken=self.token,
|
|
)
|
|
login_data = urlencode(params)
|
|
request = Request(login_url, login_data)
|
|
response = urlopen(request)
|
|
if response.geturl() == login_url:
|
|
raise Exception("Invalid password")
|
|
return response
|
|
|