Skip to content

Instantly share code, notes, and snippets.

View cjgiridhar's full-sized avatar

Chetan Giridhar cjgiridhar

View GitHub Profile
@cjgiridhar
cjgiridhar / tornadocrudclient.py
Created September 10, 2012 17:37
Tornado - CRUD Web Service - Client
import httplib2
from urllib import urlencode
import tornado.escape
import tornado.httpclient
http = httplib2.Http()
print "\nView welcome blog - GET"
headers, content = http.request("http://localhost:7777/blog/1", "GET")
print "Response:", tornado.escape.json_decode(content)
@cjgiridhar
cjgiridhar / tornadocrud.py
Created September 10, 2012 17:36
Tornado - CRUD Web Service
import tornado.ioloop
import tornado.web
from datetime import datetime
import urlparse
from bson.json_util import dumps
import pymongo
from pymongo import Connection
class Home(tornado.web.RequestHandler):
@cjgiridhar
cjgiridhar / mongoJSON.py
Created September 10, 2012 09:53
MongoDB - BSON to JSON
import pymongo
from datetime import datetime
from pymongo import Connection
conn = Connection()
db = conn['myDB']
collection = db['language']
#Creating a collection
db.language.insert({"id": "1", "name": "C", "grade":"Boring", "timestamp":datetime.now()})
@cjgiridhar
cjgiridhar / tornadoSOAPclient.py
Created September 6, 2012 08:32
Tornado - SOAP Client
import suds
## Invoke add method of MathService
url = 'http://localhost:8080/MathService?wsdl'
client = suds.client.Client(url,cache=None)
print client.service.add(2,3)
print "Request sent"
print client.last_sent()
@cjgiridhar
cjgiridhar / tornadoSOAP.py
Created September 6, 2012 06:59
Tornado - SOAP Web Service
import tornado.httpserver
import tornado.ioloop
from tornadows import soaphandler
from tornadows import webservices
from tornadows import xmltypes
from tornadows.soaphandler import webservice
class MathService(soaphandler.SoapHandler):
@webservice(_params=[int,int],_returns=int)
def add(self, a, b):
@cjgiridhar
cjgiridhar / tornadoauthgoogle.py
Created September 4, 2012 14:00
Tornado - Authentication - Google - OpenID
import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.web
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
user_json = self.get_secure_cookie("user")
return tornado.escape.json_decode(user_json)
@cjgiridhar
cjgiridhar / login.html
Created September 1, 2012 17:25
Tornado - Redis
<html>
<title>
Redis Example
</title>
<body>
<form action="/login" method="post">
Username: <input type="text" name="username">
Password: <input type="text" name="password">
<input type="submit" value="Submit">
</form>
@cjgiridhar
cjgiridhar / tornadoxsrf.py
Created August 31, 2012 13:06
Tornado - XSRF
import tornado.ioloop
import tornado.web
class Page(tornado.web.RequestHandler):
def get(self):
self.render('xsrfform.html')
def post(self):
name = self.get_argument("name")
self.write('Helo ' + name)
@cjgiridhar
cjgiridhar / _xsrf.txt
Created August 31, 2012 13:04
Missing _xsrf
WARNING:root:403 POST /page (127.0.0.1): '_xsrf' argument missing from POST
WARNING:root:403 POST /page (127.0.0.1) 1.49ms
@cjgiridhar
cjgiridhar / tornadomongorequest.py
Created August 30, 2012 11:17
Tornado - Mongo - Client
import httplib2
from urllib import urlencode
h = httplib2.Http()
## Add articles
data = {'id':'1', 'author':'B', 'genre':'comedy'}
body = urlencode(data)
h.request("http://127.0.0.1:8888/articles", "POST", body=body)
data = {'id':'2', 'author':'A', 'genre':'tragedy'}