Consuming HTTP Services in Python Transcripts
Chapter: HTTP services with Python builtins
Lecture: Introduction to Python's builtin HTTP clients

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Where are we in the course? Well, we are still in the http client area, but we are going to be focusing on the builtins, that is urllib2 and urllib
0:12 depending on whether you are on Python 2 or Python 3. Actually, there is a little bit of confusion about that, which is better,
0:19 should I use urllib or should I use urllib2, well, that kind of depends on whether you are working with Python 2 or Python 3,
0:26 so here we are in the documentation for Python 2 and notice that we have a urllib,
0:32 it says open an arbitrary network resource by url, okay, that's cool, maybe I should use this to talk to services if I want to just use the built in,
0:40 oh wait, there is also urllib2 which is urllib but even better, maybe twice as good,
0:47 I don't know, what the 2 stands for, just the next generation, urllib opening library.
0:51 So, this is a weird sort of historical confusion that you might run into just if you are doing Python 2, so we are going to focus on urllib 2
1:01 and we are going to basically disregard urllib from Python 2. Okay, well that said, we are going to see that
1:08 we are actually going to use urllib in Python 3, so let's compare these side by side to make sense of this.
1:15 Python 2 has two versions an older version urllib and the new version you just saw urllib 2,
1:20 so that is what we should work with, if we are doing Python 2; In Python 3, they made a number of breaking changes
1:26 from going from 2 to 3, one of them was that is kind of crazy, why do we have all these versions of urllib,
1:31 we are just going to create a new even better urllib that is the Python 3 urllib, and along that path they actually decided to refactor
1:40 and break apart the urllib module into a number of pieces, this included urllib.request where you actually will use that
1:48 to do the request and urllib error for checking for errors and there is a few other urllib submodules as well in Python 3.
1:57 Now, the question you might ask is why would I use either of these when I could use requests, what you are going to see, not so much here,
2:05 but dramatically so as we get in farther down the line, is that these APIs are not as clean and simple and intuitive as the request APIs,
2:16 so if you got to choose, use request, there is absolutely no doubt about use request, use request, like so many people are making that choice,
2:26 that is why it's downloaded seven million times a month, but, if you are going to write a Python app or script,
2:32 and you need to do a little bit of network work, http work or something like this, and it has no dependencies whatsoever, except if you use request
2:41 that now has a dependency, getting it installed is trickier and so on, if you want to write a script that literally has no dependencies,
2:48 you might consider using urllib if that is the one thing that would have pushed you into having external dependencies.
2:56 That said, it's up to you whether or not you use request or you use this style,
2:59 I'll show you this in Python 2, I'll show you in Python 3, but if you got a choice, absolutely use request, it's a better API.
3:06 Finally, before we dig into the details of these two different APIs, it's worth pointing out that the Python core developers
3:13 and the language team met and considered whether they might make request actually part of the standard library, they might bring request into here
3:22 to basically replace urllib in Python 3 and they decided not to, not because it wasn't important enough to sort of stand parallel
3:33 to the urllib story in Python 3, but they said look we only ship new Python distributions infrequently, and we don't want to hobble the package request
3:45 and tie it down to these various slow release dates especially because you are working on the network,
3:50 there might be network security issues or voulnerabilities that you need to quickly roll out a new version of request for
3:56 and you don't want to wait for CPython to rev entirely. So it's just interesting to think that the Python team
4:01 actually considered making request part of the builtins of Python and choose not to so as to keep request live and vibrant.

Talk Python's Mastodon Michael Kennedy's Mastodon