Monday, August 17, 2009

Very Simple RESTful Web Services in Python

I was trying to find something for easy implementation of web services in any CGI language. Firstly I considered PHP. A lot of negative emotions, nothing really simple and a wasted day. Perl has some better support for the stuff, but I've chosen Python (mostly because I've got less experience in it and wanted to give it a try).

Python has a CGI-like standard called WSGI, which makes Web Services implementation much easier. But still not that easy, as I want it. So, spent a day writing my own "library", which you can find here, hosted on Google Code. Now you can write RESTful Web Services like this:
@url_pattern("/users/${username}/plans/${year}", ['GET', 'PUT'])
def get_plans (username, year, request):
return "Inside get_plans('%s', '%s')" % (username, year)
I'm gonna use it to implement a very first version of Pomodoro Server. Some brief impressions:
  • Python is very easy to learn and has a lot of great metaprogramming capabilities, very similar to Ruby. Great language, it's a pleasure to code it!
  • Python's documentation is just awesome!
  • Some Web Frameworks provide the similar functionality, but it's painful to install and comes with a lot of other heavyweight features, such as MVC and ORM.
  • Google Code is a nice place to host your projects, though there are some limitations (for example, wiki is very basic and not compatible with anything else).
Update: I've just found a similar thing in Java, defined in JSR-311: JAX-RS - Java API for RESTful Web Services and implemented as Jersey. Just a short sample from its tutorial:
public class UserResource {
public String getUser(@PathParam("username") String userName) {
Update2: found a very similar solution for Python called Bottle. Sample code:
def hello_name(name):
return 'Hello %s!' % name

run(host='localhost', port=8080)

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java EE Training in Chennai. or learn thru Java EE Training in Chennai Students.