I just saw this json-rpc recipe go by as a popular link on del.icio.us. It’s yet-another-*Server based recipe (BaseHTTPServer, XMLRPCServer, etc). I don’t know why people keep writing these. WSGI is in all ways easier, clearer, and more useful.
So I figured I’d give it a go myself, using WebOb. Then I figured it might make a good document, and I annotated it and turned it into a tutorial. It’s also an example of using WebOb as a client library.
I’ve added several tutorials in the past months, which I thought I should also point out. The wiki example is fairly pedestrian, but shows how to do typical application-style development with WebOb. A more interesting example is the comment middleware example, which shows how much easier it can be to write middleware using WebOb than traditional WSGI middleware.
I think WebOb’s particular strong points are with middleware (where it makes middleware vastly easier to write) and web services of various kinds (RESTful or not).
Automatically generated list of related posts:
- WebOb decorator Lately I’ve been writing a few applications (e.g., PickyWiki and...
- WebOb I’ve have it in my head to extract/rewrite parts of...
- What Does A WebOb App Look Like? Lately I’ve been writing code using WebOb and just a...
- WebOb Do-It-Yourself Framework My old do-it-yourself framework tutorial was getting a bit long...
- Making a proxy with WSGI and lxml You can use WSGI to make rewriting middleware; WebOb specifically...
Thanks for doing the tutorial – that’s a big help.
“I don’t know why people keep writing these”
As someone recently guilty of writing a new RESTful “microapp” using BaseHTTPServer, some reasons;
If you still have some drive to write on this topic, in particular, for point b, some kind of decision tree would really help. E.g. WebOb – is that the recommended “equivalent” to BaseHTTPServer?
It will generally be easier to write to straight WSGI (not using WebOb) than to write to BaseHTTPServer. And wsgiref.simple_server is in the standard library. In Python 2.4 you’ll still have to install it, of course.
As to equivalents: wsgiref.simple_server is really the equivalent to BaseHTTPServer. You could write this without using WebOb, and I think it would still be simpler. BaseHTTPServer kind of has a weird little framework in it, which confuses things a bit — but that’s part of why you should use WSGI, where these different things are not confused. With WSGI, servers serve, and they don’t do anything else. They don’t dispatch, they don’t look at the request method, they don’t do any of that.
One problem might be that the docs for wsgiref (http://docs.python.org/dev/library/wsgiref.html) don’t have an introductory section that writes a small server. Instead, there are 4 paragraphs of general overview and a link to wsgi.org, then it describes wsgiref.util and .headers, which are pretty low-level and boring.
There’s an example in the last section, but that’s very far down; someone skipping through the library reference won’t read down that far. BaseHTTPServer’s docs, on the other hand, have an example in the opening section.
Thanks
Ian….
Your json-rpc example is very appreciated. But you state “In fact I don’t like JSON-RPC. It’s unnecessarily un-RESTful, and modelled too closely on XML-RPC.”
Can we get an example that is restful?
Thanks
Sam: a more RESTful API would be one where the method is determined by the URL, and then the arguments are POSTed to that URL. (Using states that are PUT/GET from a URL would be better, but at least identifying the actor with the URL is an improvement).
Even doing something like
/RPC?method=add
is more RESTful than what JSON-RPC is doing.OHM is a library I wrote to make exposing a single object easier: http://pythonpaste.org/ohm/