397 points · by frzn · 15y ago · 85 comments
Requests allow you to send HEAD, GET, POST, PUT, PATCH, and DELETE HTTP requests. You can add headers, form data, multipart files, and parameters with simple Python dictionaries, and access the response data in the same way. It’s powered by urllib2, but it does all the hard work and crazy hacks for you.
The support for keep-alive is particularly welcome, huge kudos to the author.
Question to users of other languages: do equivalents exist for PHP, Perl, C# or Java? Ruby folks rave about restclient, but is there anything else?
use Mojo::UserAgent;
print Mojo::UserAgent->new->get('http://freegeoip.net/json/8.8.8.8')->res->json->{country_name};
Very powerful.Also check out the command line interface which allows you todo things like this:
$ mojo get http://mojolicio.us 'head > title'
ref: http://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#Comm...https://github.com/tarcieri/http
httparty does a few of the same tricks, but I still find its API a bit obtrusive. At least it automatically parses JSON! (my library does this too, if an appropriate library is loaded)
In addition to a humane interface, it will soon be backed by a Ragel-generated HTTP parser:
Probably others.
I do have a half-baked implementation on urllib2 that I hate. This is really cool.
The new System.Net.Http.HttpClient in .NET 4.5 is closer to Requests and not entirely bad, if a little rigid.
Also I couldn't tell if there is keep-alive support, is there?
Caching: I think that will be best be served by a hook/helper module, but we'll see.
This looks like all the other wrapper methods you spend 5 minutes doing, once, when you start a new codebase?
We may agree or disagree with the consensus of the masses, but the masses make the front page.
I'm glad when language quirks and libraries get linked, it's good for discussion. (And more informative than a good third of the stuff that gets linked.)
I gave a talk about this very thing at PyCodeConf this week. Here are the slides: http://python-for-humans.heroku.com
Also check out Mojo::UserAgent (https://metacpan.org/module/Mojo::UserAgent) for a nice(er) alternative. And don't forget that Perl now comes a nice lightweight alternative as standard: https://metacpan.org/module/HTTP::Tiny
Apparently 113,798 people found a reason to download the 3.2.2 Windows installers (32 and 64 bit) from python.org through only the first 10 days of the month of October. http://www.python.org/webstats/usage_201110.html
(doesn't include mention of the 3.2 source tarball downloads, which are under 5,000, most likely since people get that from their distro anyway)
On a side note: last time I checked urllib2 (which actually drives Requests) could not do SSL requests over a proxy, which gave me the feeling that this implementation still isn't very mature.
One thing at a time :)
It's pretty easy to implement, as it already works at the lower (socket) level with the ssl module, it just hasn't made it up the stack into urllib2 (or Requests..yet!).
I'm still fairly new to gevent and I'm curious if others consider this good style for a library?
So yeah, this is suboptimal, but not the worst thing in the world. It should really be documented though.
File "/usr/lib/python2.6/cookielib.py", line 38, in <module>
from calendar import timegm
ImportError: cannot import name timegmfor me.
On two different machines, one OSX SL with Python 2.6 from MacPorts and the other Ubuntu 10.04.3 LTS with Python 2.6 via aptitude.
Can not find any solution for this via Google and its siblings.
I'm stuck :(
-jsl
This is false. A project released as GPL can also be released on another (commercial) license.
Most existing Python modules for sending HTTP requests are extremely verbose and cumbersome. Python’s builtin urllib2 module provides most of the HTTP capabilities you should need, but the api is thoroughly broken. It requires an enormous amount of work (even method overrides) to perform the simplest of tasks."
E.g., urllib/urllib2 both support urlopen(url, [data]) if `data` is supplied the request is executed as a POST, if not it is executed as a GET.
Issuing a GET w/ a querystring requires mutating the `url` and omitting `data`. This surprised me at first, and probably a number of others