Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created September 10, 2012 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjgiridhar/3692393 to your computer and use it in GitHub Desktop.
Save cjgiridhar/3692393 to your computer and use it in GitHub Desktop.
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)
print "Headers:", headers
print "\nCreate new blog - POST"
tags = ['python', 'web']
category = ['www']
data = {'title':'Tornado Web Server', 'tags': tags, 'category':category}
body = urlencode(data)
headers, content = http.request("http://127.0.0.1:7777/blog/", "POST", body=body)
print "Response:", tornado.escape.json_decode(content)
print "Headers:", headers
print "\nView all blogs - GET"
headers, content = http.request("http://localhost:7777/blogs/", "GET")
print "Response:", tornado.escape.json_decode(content)
print "Headers:", headers
print "\nUpdate blog - PUT"
title = "mongo"
tags = ['python', 'DB']
category = ['db']
data = {'title': title, 'tags': tags, 'category':category}
body = urlencode(data)
headers, content = http.request("http://127.0.0.1:7777/blog/2", "PUT", body=body)
print "Response:", tornado.escape.json_decode(content)
print "Headers:", headers
print "\nDeactivate blog - DELETE"
headers, content = http.request("http://127.0.0.1:7777/blog/2", "DELETE")
print "Response:", tornado.escape.json_decode(content)
print "Headers:", headers
print "\nView all blogs - GET"
headers, content = http.request("http://localhost:7777/blogs/", "GET")
print "Response:",tornado.escape.json_decode(content)
print "Headers:", headers
print "\nClear all blogs - DELETE"
http = httplib2.Http()
headers, content = http.request("http://127.0.0.1:7777/blogs/", "DELETE")
print "Response:", tornado.escape.json_decode(content)
print "Headers:", headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment