Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #7 from ryuslash/use-python-requests
Browse files Browse the repository at this point in the history
Use python requests
  • Loading branch information
callahad committed Jun 28, 2012
2 parents 503c759 + 5328f87 commit 6b5292f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
3 changes: 2 additions & 1 deletion python/django/README.md
@@ -1,6 +1,7 @@
# Python django example

This example was written using django 1.4.
This example was written using [django](http://www.djangoproject.com)
1.4 and [Requests](http://python-requests.org).

To run this example:

Expand Down
22 changes: 15 additions & 7 deletions python/django/example/browserid/views.py
@@ -1,6 +1,4 @@
import urllib
import urllib2
import json
import requests

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
Expand All @@ -11,9 +9,19 @@ def status(request):

audience = 'http://localhost:8000'
assertion = request.POST['assertion']
page = urllib2.urlopen('https://browserid.org/verify',
urllib.urlencode({ "assertion": assertion,
"audience": audience}))
data = json.load(page)

try:
page = requests.post('https://browserid.org/verify',
verify=True,
data={ "assertion": assertion,
"audience": audience})
data = page.json
except requests.exceptions.SSLError:
data = { "status": "failed",
"reason": "Could not verify SSL certificate" }
except requests.exceptions.ConnectionError:
data = { "status": "failed",
"reason": "Could not connect to server" }


return render_to_response('status.html', { 'data': data })
21 changes: 15 additions & 6 deletions python/python.cgi
@@ -1,10 +1,8 @@
#!/usr/bin/python

import cgi
import json
import os
import urllib
import urllib2
import requests

def print_login_form():
print """<html>
Expand Down Expand Up @@ -38,10 +36,21 @@ def verify_assertion(assertion):
if 'HTTPS' in os.environ:
audience = 'https://'
audience += os.environ['SERVER_NAME'] + ':' + os.environ['SERVER_PORT']
postdata = urllib.urlencode({"assertion": assertion, "audience": audience})

page = urllib2.urlopen('https://browserid.org/verify', postdata)
return json.load(page)
try:
page = requests.post('https://browserid.org/verify',
verify=True,
data={ "assertion": assertion,
"audience": audience})
data = page.json
except requests.exceptions.SSLError:
data = { "status": "failed",
"reason": "Could not verify SSL certificate" }
except requests.exceptions.ConnectionError:
data = { "status": "failed",
"reason": "Could not connect to server" }

return data

print 'Content-type: text/html\n\n'

Expand Down
21 changes: 14 additions & 7 deletions python/web.py/browserid.py
@@ -1,9 +1,7 @@
#!/usr/bin/python2

import web
import urllib
import urllib2
import json
import requests

urls = (
'/', 'index',
Expand All @@ -21,10 +19,19 @@ class status:
def POST(self):
audience = "http://localhost:8080"
i = web.input()
page = urllib2.urlopen('https://browserid.org/verify',
urllib.urlencode({ "assertion": i.assertion,
"audience": audience }))
data = json.load(page)

try:
page = requests.post('https://browserid.org/verify',
verify=True,
data={ "assertion": i.assertion,
"audience": audience})
data = page.json
except requests.exceptions.SSLError:
data = { "status": "failed",
"reason": "Could not verify SSL certificate" }
except requests.exceptions.ConnectionError:
data = { "status": "failed",
"reason": "Could not connect to server" }

if data['status'] == "okay":
message = "Logged in as: %s" % data['email']
Expand Down

0 comments on commit 6b5292f

Please sign in to comment.