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