Hello and welcome to this edition to the way the programmer.
So, my name's Charles Severance.
I teach Python courses.
I teach web vellum and courses.
So, I was tasked with giving you a real-world example of object orientation.
This is a little difficult for me because in the real world,
little tiny programs tend to not
cause us as programmers to build or even use objects in a sophisticated way.
So, I've decided in this that I'm going to try to take you into
a lot more complex world and give you a sense of what's going on behind
the scenes and how object orientation is essential.
So, I'm going to actually use Django which Django is a web development framework.
This is less about writing Django than it is about looking at how
Django uses objects to interact with you as a programmer who's going to write Django.
So, if you really want to go crazy on this,
go to my website dj4e.com,
which is under construction.
I'm not really teaching this course until a couple of months from now.
But this will be eventually by the time you're watching this,
maybe this'll be far more sophisticated.
The thing I'm working through is what's called the Django tutorial.
This comes from the Django community.
This is where a lot of initial Django developers, they just say,
"Hey, you want to get started,
go ahead and get started."
So, we're going to go through like the first one and two.
But I've already done that.
Again this is not about Django,
this is about object orientation.
So, I'm going to play with these things and show you some of the source code.
So, it might be good if I showed you
the eventual source code that I'm trying to weave a narrative that we get to.
So, let's take a look at that source code.
So, if I go into polls two,
this is a little application.
It doesn't do much.
Right now it just prints out hello.
I got to start the server, hang on.
So, I have the server running.
This little application.
I got to go up a directory. Now, it's running.
So, this little application that it's only at the second step of
the tutorial and the only thing that it does is print something out.
As such it's a not a very sophisticated application.
I'm going to talk to you about three files.
The first file is a file called the URLs file.
The URLs file is very simple and it basically says,
for this particular path which is the path that has nothing here after that slash,
run this code from the file views with a method named index.
So, if I look at views,
this is the view code.
It basically is saying,
return an HTTP response because it's part of the request response cycle.
Hello world and that prints this thing out.
It's calling a bunch of things.
Believe me and I'll show you a little bit at how tough this would be,
if we didn't have Django doing all this work.
So, this is the code that we have to write.
Then the other thing that we're not going to cover too much
about is how to create database tables.
Here's this model file in this class.
Basically, says let's make a question model which is also then a table in the database
and that it is extending an object that comes from Django called models.model.
If you look down, this is the code that I've got to do and
it has the crates fields in the database and all these other things.
If I took a look at what this database is,
it ends up with all this other stuff and has all these files that are created for me.
There's data in these files.
I mean there's a structure in these files et cetera.
All I did was, created this.
So, there's a sort of a very small amount of work that I
had to do to get Django to do a whole bunch of work for me.
I do that by extending an object that I'm handed,
this models.model object is something that I'm extending.
So, at the end of the day,
what I want to do is I want to look at what it takes to
take a URL and have a piece of code that responds to that URL.
So, here's some URLs.
I want to show you how Django works and what Django gives you
in a way that makes building Django code way more easier than it could be.
So, if I was to build
a web server and I've written here and I'll show you this code in a second.
I've written the simplest raw Python web server that I could write.
Basically, at some point,
a browser's going to type the URL.
It's going to send a request over the Internet to your computer.
In that computer, there is like an operating system whether it's
a Mac or a Windows or a Linux and it has networking capability TCP/ IP.
There are documents that if you really wanted to,
you would learn how to build those things.
So, you can type into Google, RFC791 or RFC793.
Then you can go get a sense of how hard that really would be.
I don't know if you've played with sockets or not,
but there is some socket or socket library inside of Python.
Then we're going to create a very simple web server that's going to receive
this request from the browser and then send back a really tiny little HTML bit.
It's going to be sent back to that browser, very tiny HTML.
Little body tag, Hello world,
slash body, slash HTML.
I'll cover all this stuff, how we create this server.
This isn't eveb a real web servers,
it's not reading files or anything else.
It responds to every request with Hello world over and, over and over again.
Then the next thing we're going to show after that is
we're going to show how those three files that I showed you,
models.py, URLs.py, and views.py.
We write 25 lines of code.
Then we've created this application.
That's very sophisticated and it's very capable.
Then after that, I'm going to show you sort of the detail of how within Django,
Django creates objects that then you extend
to communicate with Django how you want your application to work.
So, we're going to start with a very low level concept
of how a Python program would talk directly to a web browser.
So, first let's take a look at this server code.
Let me start a new browser,
let me actually get rid of the Django server can stay there.
You can download this from djquery.com,
it's a little smaller so we can see it all.
Okay, so if you were here,
some stack overflows that I used,
and some Python documentation that I use to figure all this stuff out.
This is not particularly an object.
This is a socket library that's built into Python already.
And then if we take look, one more line.
I have to put a try except around all that stuff.
So we can call it, we're going to print.
Here's how you go get this thing.
We're going to call this to create server function,
and server socket, we're telling it to start a socket,
a sockets little different than a file.
It's kind like a file handle,
except that's two-way thing which you can both read and write to the same thing.
It's you can write to a file because it's like two sides.
If you have one application,
and they're both connected via socket one side can write,
and then the other side can read,
and then the other side can right, and it comes back.
So it's like, it's almost like two connections.
A socket is a bidirectional,
or a two-way connection.
So we're starting a socket,
this is the server side because in Django you're building the server side not a browser.
Browser is the client side, this is the server side.
We wrap this whole thing in a try except,
because I need to close these sockets,
if you're writing this code,
took me about an hour to write this code,
even after I found it on stack overflow.
So you don't really want to write this. I'm glad it worked.
You can look at it, you can play with it,
but all it really is it's whole goal of
this code is to prove to you that you don't want to write this code.
And so I'll just walk through it.
What we're doing is,
we're going to be a server which means we're waiting for connections,
and that's what serversocket.bind,
says wait for connections on port 9,000,
serversocket.listen five, that means up to five connections.
Then this while loop basically says,
accept the socket if the socket has arrived.
So it'll actually stop here until a socket connection happens.
So when a browser makes a connection to your server,
it sits here well,
it sits here until the browser makes a connection, and then it drives down,
and it gets these two pieces of data which are a tuple,
but it is just an assignment statement.
Client socket address.
The address of where what the browser's coming from,
and the socket objects.
So this is now an object.
So client socket is an object that's populated by the statement.
So if you were to read the HTTP protocol you would
realize that the first thing that browsers are going to do is send us a command like get,
or post with a URL that it's interested in.
So we're going to read the first 5,000 characters from the browser,
and then we're going to decode it.
I'll split based on new lines,
because it's actually a series of lines both the HTTP command,
and then a series of header values.
We're going to print the very first line which
you're going to see how this works in a second.
And then we know what the HTTP protocol is,
so we're going just send back the simplest of HTTP pages which is a 200 OK,
which says this is a valid document here you are.
We're going to send back UTF8 html, and then a blank line.
So here's the blank line right here.
They're going to send the body with two new lines at the end.
Of course because we're sending it outside of our world,
we're going to do a data.encode,
so that the UTF8, is properly sent.
So we're going to receive UTF8,
that's where this decode came in.
The get request that comes into us,
is going to be UTF8.
Then we're going to send back a document that's also UTF8.
That's also why I told it, it's UTF8,
and then encode is what converts the unicode inside a Python into UTF8.
And then we have to kind of shut it down,
which the shutdown sort of sends all the data,
make sure the data gets received,
and then take the socket.
Then we go back up to the top,
and we wait for the next sockets.
So that's each one of the URLs that gets retrieved is going to be handled.
So if keyboard is apply,
hit Control C or Control Z.
It shuts down, but closes the socket turns out it
takes a minute or two to be able to start it back up again unless you do it.
Or if like you type in change in this
you make a mistake you project yourself an exception,
and we still want to close the socket,
if we made a mistake inside here.
That was really helpful for me because it took
me a long time to get this code work, and it blew up a lot.
So now it's not blowing up. So now let's run this.
I can say Python three server.py.
Now, we have the world's simplest Python server.
So if I go into my browser,
and I go to local host,
localhost 9,000, that's the ports like an extension on a phone system.
So localhost: 9,000, says connect localhost which is the same computer that I'm on,
does not really leaving to the internet.
That would be a normal domain name,
: 9,000 says on 9,000,
and my Django was running on 8,000,
and my little mini Python server is running on 9,000.
So I'm going to copy that.
I'm going to go into Google Chrome.
I'm going to turn on developer console,
and then I am going to turn,
and watch the network.
Then I'm going to type in that URL.
That's just junk.
So we don't want that, I don't want that too.
Someone hit enter. If all goes well.
So it says hello world,
see that, pretty awesome, after all that work.
So what happens is you go down here you can see that, where is this I don't want to see.
I don't want to see that.
So we send a request to the localhost on port 9,000,
we did a GET 200 OK.
Remember our application said 200 OK, where is that was,
see this 200 right there, 200 OK,
was the status that our browser got back from our application.
The response headers.
The response headers said that this is a html document with a character said of UTF8.
"Oh looking looking looking where you did that."
We said that in our Python server.py code,
and request headers, this is all that stuff.
This is the browser telling us what kind of browser it is et cetera, et cetera.
Where that shows up is, remember I did this split,
by only showing pieces sub zero.
I'm not showing you any of those things,
but if you just were to print out this rd variable in your version of it,
you would actually see all of those headers,
and that's the HTTP request response cycle.
That's like the simplest diversion of the HTTP request response cycle.
Now, we take a look at this, we'll see that this is also
retrieving the document favicon because it's trying to return the icon.
That icon would show up right here,
and our web servers is not smart enough to give back icons.
It only says hello world to every single request,
because it went around twice,
it did this for the localhost,
slash, and then favicon.
It did it again, and it simply sent back a document.
Now, after awhile we can say this char type it is image JPEG.
Then we would have different body,
and then we could send favicons back and forth.
If you'll notice the prints out at this print statement right here.
This print statement right here,
prints out the GET request which is,
this is the first request that corresponds to that localhost.
This is the second request that corresponds to that favicon.
So we got two requests,
and then I'm going to blow this up,
and that's when it's going to hit the keyboard interrupt.
Then it's going to shut down,
and then it's going to finish that.
So that server.py was,
how a web application works.
But this is like such a tiny fraction.
If you really want it to build a real live web application that had multiple files,
and CSS, and images, and all that stuff.
It gets harder, and then receiving data,
there's parsing of the incoming data.
If you google, like the RFC for HTTP,
RFC stands for request for comments.
You will find RFC 2616, Hypertext Transfer Protocol.
That gives you some subset of
how much information you need to know to write a web server,
and to write a web browser.
So I'll just start to skim down.
We got hundreds of pages.
That was just a table of contents goes down to 176.
This is one of many documents that describes how that works.
So it turns out that code that I wrote is not entirely compliant,
is just barely good enough to do those most crude,
and simple version of this.
So there you can look at it, and you can play with that,
you could add print statements et cetera,
et cetera, et cetera.
So if you are going to want to write an entire browser,
you have to figure all these things out in Django like
the 2616 Hypertext Transport Protocol.
You'd have to write all this.
There's all these other rules.
So I think what I'll do is I'll stop now,
and then coming back I'm going to show you how Django implements that exact same thing,
but then how your code hooks into Django in a way,
where you're using object oriented programming to hook
to code into Django in the right places.
So that the monocode that you have to write is really small,
and the amount of documentation that you have to read is extremely small.
Then someone inside Django had to read lots,
and lots of things about how to make this stuff work.
So when we come back,
we'll talk a lot more about your relationship between the Django code,
and your code, and how that relationship has done through object-oriented programming.