[Tutor] preferred httprequest library
Stefan Behnel
stefan_ml at behnel.de
Fri May 9 10:54:21 CEST 2014
Martin A. Brown, 09.05.2014 00:54:
> : I¹m new to python but not so much to programming. I need to
> : construct a set or programs to test a forms poster that has been
> : enhanced (it is in php). I mostly need http get and post. This
> : is a hands on set of tests and does not have to be bullet proof.
> :
> : What would be a good http request library to use for this work?
>
> There are many options. When you can afford to suck the entire
> remote resource into memory (as with many applications), you will
> probably find the 'requests' library very handy. I'd start here,
> avoiding grabbing for the standard library tools (urllib, and
> urllib2) unless you need that finer control.
>
> This has a nice abstraction and, from your description, I think this
> would be a good fit:
>
> http://docs.python-requests.org/en/latest/
Agreed that "requests" is a good external tool with a (mostly) nice
interface, but if you really just need to do GET/POST, there's nothing
wrong with the stdlib's urllib.request module (called urllib2 in Python 2).
https://docs.python.org/3.4/library/urllib.request.html
Basically, you just say
result = urllib.request.urlopen(some_url)
for a GET request and
result = urllib.request.urlopen(some_url, data=post_data)
for POST. It returns a file-like object that you can use to read the
response, ask for headers, etc.
Advantage is that it runs out of the box on whatever Python installation
you have. If installing external packages before running the tests is not
an issue, then the "requests" library will make your life a bit easier for
the more involved cases that you might encounter at some point in the future.
Stefan
More information about the Tutor
mailing list