Whenever looking at API libraries for Python, there seems to be about half of them simply using:
response = urllib2.urlopen('https://www.example.com/api', data)
and about half using:
connection = httplib.HTTPSConnection('www.example.com/api')
# ... rest omitted for simplicity
I tend to think the second version is "cooler" (I'm biased towards a more OO approach to most things).
Is there a benefit or reason for using one over the other. Or, am I missing something along the way. I would suspect that urllib2.urlopen
uses HTTPSConnection
in its implementation, so perhaps one is simply less coding on my behalf. Whichever way, I'd love some feedback. Thanks.
Yep, urllib2
uses HTTPSConnection
(or whatever kind of connection is appropriate for the protocol) in its implementation. It's basically just a shortcut to do the most common thing people do with httplib
.
urllib2
also has some code to handle things like redirects and authentication requests, all stuff you might have to code manually if you were doing it with plain httplib
.
EDIT: In response to Michael's comment, if you were wondering about object vs. data... it sort of depends on what you're going to do with it. If you need the connection object (e.g. to do something special with it, maybe a keepalive connection), then sure, go ahead and use the httplib
way and return the connection object. But if you're just trying to get the data, just get the data and return it. If you like OOP, know that everything in Python technically is an object; what you get from urllib2.urlopen
is a file-like object which has methods to retrieve its value as a string object or as a list of lines (also string objects). I think most Python programmers would consider it a waste of effort to use httplib
in the latter case.