Consuming HTTP Services in Python Transcripts
Chapter: HTTP services with Python builtins
Lecture: Python 2: Blog explorer with urllib2

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Here we are back in PyCharm, and this is something like the blog explorer that we just wrote in request in Python 3,
0:10 now, we are going to run this in Python 2 and we are going to use the builtins, so we are going to need to do import urllib, there is the two versions,
0:18 this is absolutely Python 2, so I am going to go with this here, and notice we are even checking to make sure it's running on Python 2
0:25 and if I go and run this, you'll see that I've created a separate virtual environment based on Python 2 that we can use here.
0:34 Okay, so let's look through the app is basically the same, of course we had to switch raw input for input and print no longer takes parenthesis,
0:41 but other than that, you'll find there is not whole lot different factors, I don't think there is anything different in this code, so we go down here,
0:48 what we are going to do is we are going do is we are going to wrote get post again, so we are going to come down, and we are going to create a response
0:55 just like we did with requests, so we are going to come down here and do something a little less obvious
1:02 but it's not too bad, so we'll say for this basic get case unauthenticated
1:05 don't have to do too much, so urllib2, url open, and we are going to pass url,
1:09 we could pass the data and things like that but we're not going to do that, we're just going to pass the url because this is a get request,
1:15 now it doesn't have a status code, this is more function driven, there is no properties or anything like that, but we can come over here,
1:21 and call get code as a function, okay and then again, if an error happens, we'll leave it like it is now, it's going to end up in a big bad crash,
1:29 so we also don't get the text this way, we say read it's kind of like a stream sort of thing,
1:34 so if that works, we should be able to get the data, now this didn't look too bad, right, so let's go ahead and try running this.
1:41 That is running in Python 2 and let's go ahead and list, oh it has no attribute json,
1:47 let's jump a little bit ahead, so this is going to have to be post data and we are going to need to do a little bit of work,
1:53 there is no builtin json here so we need to go and use the json module
1:57 which we have to import and then we have to have this say load s to parse a string, we've already talked about this in previous videos,
2:05 and then we give it the response.read text and maybe it's even worth writing this as a separate variable so we can check it out if we need to.
2:11 Okay, try again, let's go list boom, easy breezy back to good, well, that seems pretty cool, maybe we can go and write the next one
2:20 without too much trouble, now one more thing that can be tricky about working with this library is it can keep these sockets open,
2:27 so it's really important that we close even though it's not in the auto-completion here that we close the socket.
2:34 Now wouldn't it be cool if this could be used in the context manager? Well, it will be, shortly. But only in Python 3.


Talk Python's Mastodon Michael Kennedy's Mastodon