Created
September 10, 2012 17:37
-
-
Save cjgiridhar/3692393 to your computer and use it in GitHub Desktop.
Tornado - CRUD Web Service - Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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