3

I am designing a service that consumes data from REST based web service. I understand that there is multiple choice available when selecting between different modules.

There is a question that compare urllib2 vs requests but that post just provide answer as use Requests rather than things to be considered.

From application architecture point of view what are factors to be considered before selecting between following modules :

urllib

urllib2

http.client

requests

My application will accept data as either JSON or in XML.

Community
  • 1
  • 1
GoldenPlatinum
  • 427
  • 2
  • 4
  • 12
  • Welcome to stackoverflow. I just want to alert you that your question sails close to the wind in terms of being "opinion based", read here http://stackoverflow.com/help/dont-ask. I hope you will get some constructive, objective answers about the facts of the matter (objective factors to be considered, not opinions on which module to use). – GreenAsJade Sep 18 '14 at 04:28
  • Thank you. I am not looking for anyone's opinion rather hard facts they considered architecture prospective before building new service. – GoldenPlatinum Sep 18 '14 at 04:34

1 Answers1

2

Use requests just for its pure simplicity. There's an informative gist on Github that compares logging in to an authenticated resource using urllib2 vs. requests. If you're working with JSON responses, requests can easily translate the response directly into a Python dict:

r = requests.get("http://example.com/api/query?param=value&param2=value2", auth=(user, passwd))
results_dict = r.json()

Simple as that - no extra json import to deal with, no dumping and loading, etc. Just get the data, translate it to Python, done.

urllib and urllib2 are just not very convenient. You have to build requests and handlers, set up authentication managers, take care of a lot of nitty-gritty that you shouldn't have to. http.client is even lower-level - its contents are used by the urllibs to do their thing, and aren't often accessed directly. Requests is getting more and more feature-ful by the day, all with the overarching principle to make things as easy to do as possible, yet allow as much customization as needed if your requirements are out of the ordinary. It has a very active development and user community, so if you need something done, chances are others do too, and with their short release schedules you may see a patch out before too long.

So, if you're mainly just going to be consuming web services, requests is an easy choice. And, on the off-chance that you really can't do something with it, the others are there in the standard lib to back you up just in case.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Thank you Matt. `requests` sound good choice. Although as I see it only downside will be that it can not process XML response. – GoldenPlatinum Sep 18 '14 at 21:22
  • @GoldenPlatinum sure it can, just like any other text response. Just feed `r.text` into your favorite XML parser and you should be all set. – MattDMo Sep 18 '14 at 21:26