views:

489

answers:

6

Hi,

I am bit confuse over set of libraries of pythons to connect with REST enabled web services. I have tried httplib, urllib and urllib2. I want to know how can methods like PUT, GET, POST, DELETE can be achieved using this library.

Regards, Parthiv

+1  A: 

This is a fairly simple REST client.

Mark B
Thanks for the reply but i just want to learn how can i make a basic REST request using libraries like httplib, urllib or urllib2
Parthiv
+4  A: 

We started with urllib2. It doesn't do everything everything you need.

However, if you subclass Request, you can add the necessary PUT and DELETE methods to the request.

You can then use your subclass of Request with urlopen.

S.Lott
+2  A: 

This should hit the spot - http://developer.yahoo.com/python/python-rest.html You can see how to make GET and POST requests using httplib or urllib2 on that site. It doesn't cover PUT and DELETE though. Another disadvantage is that it is specific to Yahoo. I think it can be adapted for what you need though.

I think that these will help as well- http://www.ezran.org/blog/2008/09/rest-and-python/ and http://confluence.atlassian.com/display/FECRUDEV/Writing+a+REST+Client+in+Python

batbrat
+2  A: 

Here is a an excellent tutorial, from Mark Pilgrim's Dive into Python 3, on which Http library to use, why and how.

Darrel Miller
A: 

you probably want to hear about http://httplib2.googlecode.com/hg/doc/html/index.html which looks like a good comprehensive library to ease dealing with HTTP.

what you probably do not want to hear is that i believe the REST paradigm to be utter bullshit. frankly no one could so far convince me this is a worthwhile idea. in short, there's many things you want to do in a web application, and those four verbs GET, PUT, POST, DELETE just don’t cut it.

i remember a lively discussion over at http://jjinux.blogspot.com/2009/02/rest-restful-shopping-carts.html about the goods and bads of REST. it was also about a guy that had so thoroughly digested the doogfood that is REST he got convinced that session IDs do not belong into a cookie, they have to go into the URL. how stupid can a single bad idea make you?

looking for that article, i stumbled over the main page of the blog, and right on top there sits a two-weeks old article, entitled: ‘Rails: The REST Religion’. here are some quotes for you:

It's a simple fact of life that Web browsers don't currently support DELETE and PUT. [...] I'm going to have to deal with browsers that don't support DELETE and PUT for many years to come. Hence, Rails' [way to implement REST] is a HACK in order to try to force the Web to be something it isn't, all the while claiming that RESTful routing is the way the Web is meant to work.

RESTful routing actually causes me far more pain than any other feature of Rails.

I wish I could abandon the idea of Resource Oriented Architectures which insist that you may use any noun you want, as long as you only use the verbs GET, POST, PUT, DELETE, HEAD, and OPTIONS. I wish I could use any beautiful verb I wanted to and shove it in the URL all willy-nilly.

REST is a real bad idea, and i hope people will stop from buying into it.

flow
"Web browsers don't currently support DELETE and PUT" doesn't seem to have any bearing on REST as used between client and server applications. Interestingly the CRUD verbs work for a database; folks seem to cope well with using them for web sites. You seem to have spent a lot of your answer in off-task comments that are confusing. Would you considering dropping the invective, focusing your real answer, and taking up the anti-REST in a separate question?
S.Lott
i want to make people wake up to the fact that REST in effect moves part of the semantics of a URL *out of* the URL: thus HTTP GET http://foo.com/delete/product/567 is supposed to become HTTP DELETE http://foo.com/product/567. HTTP request types are a historic relic of a time when it was felt that file transfer or a WHOIS query would necessitate an own port and an own protocol. REST is HTTP stoneage.CRUD is a long-standing and successful metaphor. but what we want is meaningful URLs. REST destroys the semantics of URLs, therefore, REST is bad.
flow
...and i think my comment is on-topic not off-topic. if a n00b asked you to teach them COBOL wouldn’t you try and tell them about better ways to learn programming? of course if the server the OP asked about only offers REST and there is no other solution, then using HTTP PUT and DELETE might be fully justified.
flow
@flow: "if the server the OP asked about only offers REST" Since we don't know otherwise, it's important to stick to the topic. You are free to post opinions on your blog and update any questions on SO that refers to the question of REST vs. SOAP. But this question presumes REST, so it helps to stick to that.
S.Lott
@flow: The COBOL thing is funny. Two years ago, I spent a fair number of weeks teaching COBOL to n00bs. It's what the customer needed. Same for REST: it works really well. Interestingly, most proponents say it preserves the precise semantics of HTTP. Your example of `delete` in the URL is a terrible thing, since it conflates verb and noun in a terrible way. `delete` as a method leaves the URL to purely identify a resource and do nothing more. In this case, it's what the question's about. So your comments sound off-task.
S.Lott
It is quite amazing that you find GET,PUT,POST and DELETE are insufficient, and yet all software in the world comes down to a bunch of ones and zeros. If a problem seems impossible, maybe you are just looking at it the wrong way.
Darrel Miller
-1 - I agree with S.Lott
Ninefingers
+1  A: 

I had the same question and found httplib2 via Dive Into Python. It supports PUT and DELETE and generally seems cleaner than urllib, urllib2, and httplib. I'm using it with good results so far.

Parand