Building a Simple HTTP Server in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we built a browser in Python it's time to build a web server in Python so remember that what we've got here is we've got you know the browser is an application and we've we've made this a Python application now that sends this get request so we we have effectively like I said built the world's simplest browser and that sends a get request that server does something and then it sends that the response back and now we're going to kind of change this right we're going to assume the browser exists and it's like okay because we're gonna be the server folks right so now we're gonna look in greater detail at what goes on in the server and so here is a very very simple web server it's a few more lines than the than the the simplest of a web browser because we have to put a little bit of error checking in with some tries and accepts okay so let's let's walk through what this code does okay let me let me change the color again to black if you're from Microsoft and you can tell them to make a key to change the color on on the scribbler I had a greatly appreciate it oh and then I went down on a page too okay so we're going to pull in some more stuff from this from the socket we're gonna pull in some stuff from the socket right here and so we're gonna make a function called create server that we're gonna call right down here and it's going to print out how you're supposed to access it and then start the server now the whole idea of the server is the trip server is woke up to wait for incoming connections so the server already exists so when you start talking to a web server that servers in that computer already the server software is already running registering its interest in incoming requests so that's what we do so when this Python program starts it's going to sit there and wait in an infinite loop for incoming requests so the first thing we do is we're going to make a socket this looks very much like the time we make it's an endpoint it's not actually making the phone call we're remember I said this socket call is making the phone so saying we're gonna make a phone and it's up to us later to decide for gonna make a phone call or receive phone calls and then this next thing is like to connect except this is I am willing on port 90 to receive the phone calls now it turns out there's only one program on port 90 that can receive phone calls so this might blow up you might try to do this I mean port 9000 and if you run this twice and you can do it in two windows on your computer the second want to blow up and say you can't have port 9000 cuz another piece of software has it but as long as there's only one running away you go and that's part of this whole try accept thing cuz it's if you run this twice gonna blow up the second time because you cannot receive phone calls on this server on port 9000 with two applications one application gets the phone calls that's what socket listen says the five on there says dear operating system if I'm busy handling one phone call you can hold on to four more and cue them and then I'll come back and get it for you and that you're asking the operating system to cue incoming calls don't just say you're busy shut down if you didn't say this listen five if you are busy writing the data for phone call one and phone call two came in and you weren't ready for it right that instant it would just deny the phone call so that's what listen says is do your operating system hold hold those temporarily I'll get back to you which is how it works and then what happens is we go into this except now this except is I'm at the phone I've registered what my number is and what my extension is and I'm ready to pick the phone up let me know so this accept is blocking it stops and it just sits there and and it can sit there forever you know just literally forever if nobody calls it nothing happens the next line never exit never runs until you blow it up or the server goes down so that's accept is a blocking and the reason we do this accept well we have to establish the phone call first right so then this next line runs only when a phone calls received so that means that we on the browser side we already connected you know we made the phone call no way I'm sending data yet right and so this is just now at this point somewhere out there there's a piece of client software that has done a socket connect and we have done accept and are accept is succeeded in the server the connect is succeeded and we are ready to talk so that phone call has been made now the question is who is gonna say hello and this is where the HTTP protocol solves our problem the server knows that the client must speak first so it just doesn't receive know you'll notice the receive in the send are the same function calls because it's a two-way thing the whatever the browser is sending the server is receiving in whatever the server sends the browser receives and they can do it simultaneously if they can figure it out but usually you kind of like to say it's your turn to listen and my turn to send and then I'll listen and usually they kind of go back and forth in this there's only one step you the server listens gets the get request sends the data back and so then we're going to read some data and we're gonna have five thousand characters where you get the whole thing remember it's one line and has a get request in it it has the optional parameters in it and so we're gonna split it now again we've received it as utf-8 we've got to decode it for utf-16 I mean on utf-16 Unicode inside of Python and then we split it based on new lines because the get request is on one line then header header header header header header and then the blank line to tell us that we're into the headers and we're only going to take in this we're just gonna look at that first line we're not gonna do anything with it most most servers actually like look at the line to figure out what document to sent that's a very simple server it sends the same document no matter what the URL is all we do with the URL is we print to it print it out to prove that we got it and then we construct a response and in this response again go back to RFC to 626 16 and it will tell you what this response is supposed to look like it sends back a 200 hundred okay remember this all the stuff we saw probably I cut and pasted it from a working thing we tell it that we're sending it back in content type a text HTML the character set utf-8 memorize telling you that a blank line and then some HTML HTML body hello world body and I'm throwing in these slash are slash ends which is the network's version of new lines and then you will notice that I encode it before I send it because it's Unicode inside of Python and it needs to be sent as utf-8 because the phone when you're in a socket the thing that's going across the socket almost always is supposed to be utf-8 so we encode it and and then because the protocol says so as soon as we send the data we close the connection and remember that the client had to close the connection too so we close it it's half closed and then the client gets all of its data gets the indication and a close knows it's finished all the data and then the client shuts it side down and so there are two hang-ups it's kind of like a phone call the person hangs up and then click you hang up now there you can't really keep talking but you do want to have both sides hang up and that shutdown is what's going on and then the rest of this is all try except for various things so we clean up our socket so we don't have to restart our server and our software blows up and so the rest of it's pretty simple and then so this y1 the first browser will talk and then it'll send some data and it'll go back and wait for the next phone call and that's what's going on the next phone call comes in it doesn't get me sees a get request it sends the data back then goes and waits for another phone call so this is an infinite loop that sits and waits for incoming phone calls and it you know it answers the same thing so it's like you call this phone number and it goes and you go I would like a pizza and it says hello world and then click and then you call it up saying I would like a car and it says hello world click and then you say I would like to register for this class and it says hello world click so this is not a very flexible server you will build far more flexible servers but it's the one I could build and fit on a single page in a slide deck so to run this if you you know you download this code and it's a tiny bit of code and you run it so you go somewhere maybe your laptop maybe Python anywhere I don't know if it works on Python probably doesn't work on Python anywhere because you can actually talk to ports but let's just run this on your laptop and you you start the server up somewhere get a hold of my sample code or download from here server dot py and it's telling you this is just output from the server and this is the moment at which it's waiting right it's waiting the server could wait for hours at this point but I just give you this in a print statement so you can copy that and put it into your browser so you paste localhost 9000 into your browser and then the server is just printing that it got a get request for the slash document from this browser that's what the browser sent I mean we're talking from Firefox browser to the little web server that we just built now there's another second cat request that you don't even know cuz you didn't ask for it and that's because the browser is built-in to ask for a URL called favicon dot ICO so that it can make an icon and that icon ends up in the tab or wherever in your browser and so that's like an icon for the website so you didn't ask the browser to do this but it's a thing browsers do and you'll see when you're looking your debug logs when you're building stuff that it's a favicon and so this this happens like blink link and now it's waiting again right and you can hit refresh or you can go start a different browser and you would see these get requests every get requests you're going to see in your browser and your server so it's uh so it's working it works really well and you're welcome to go ahead and play with it and try it all out so now we're going to do is build a simple web client and that will talk to our server so the other web client we may talk to dated a PR for you org this one is going to talk to yourself so it's this it almost looks like entacle right we're gonna make a phone call we're making a phone we're going to connect to localhost one 2700 one is the ipv4 connection what's called loopback right - the same host we're talking to port 9000 because that's where we've got our webserver running that little webserver we just wrote and then we're gonna send a get request the valid gut requests to this thing h2o and zero and then two new lines and we're going to encode it into utf-8 before we send it then we send it and then we just have a loop to print it all out and then when the socket gets closed we hit a break and then we close our end of the socket so the server clicks the phone and then we're know we're done with our data but we've already printed it out and then we hang up the phone on our end to kind of clean everything up all the way in between everything else so this is a very simple web client and so now you can run the web server right just like we did before and instead of talking in a browser we run this client and this client gets a header of HTTP one okay it's content type XHTML it gets a new line and then the body and we're done now I mean it's not a browser but it is client that's talking HTTP and so now you'll notice that this server sort of sits in waits for incoming calls you could run this over and over on the client run the client over and over again right and then you would see more requests but in general the server is destined to just sit and wait for hours potentially until a client connects to it and then at some point you'll just abort this server and then if you control C or blow this up somehow this will fail the client will fail and it will fail it will fail in this connect code so if you want to play with it try to run this client without running the server and you'll see that the connect which is the part of actually making the phone call this will continue to work which is making me a phone on my current computer and then connect is make a call to the remote computer that will blow up and then if you wanted to play with this you can put some try and except surround it say remote computer is not responding on port 9000 that's if you blew up you put could put a try and except around that but I'm not doing it because you don't fit on my slides so here is an even simpler web client that we can use and you again you can get this code from my sample code we don't talk to this we there's a higher level thing that so the the previous client talked to sockets so I could show you the low level but because we spend so much time talking about two URLs we then have a URL em so I'm just gonna use URL live I'm gonna and this could be a localhost or it could be whatever I'm just gonna make a four line you are a Lib call and so I just do a URL open of that same URL now we're operating at the HTTP level because URL concept of URL is not a socket concept a concept of URLs HTTP concept then we just get in effect what looks like a file handle to us and we loop through it and print it all out so we run our server and the server sort of says this and waits and then we run our client it basically hits our server and we can see in the server window you got to do these two things in two windows right you got to do these things in two separate windows so in the server window it'll sit and wait and then in the client window when it runs you'll see the client will make the request to the server or blow up with the server not running and the server will see the get request and then the data will come back to the client so you sort of do this in two things and you can run the client over and over and over again and you'll see every time you run the client the server sees it responds and away you go and again our server always says hello world no matter what which is delightful and classic it's a classic so you actually can watch when you're running django there will be a way for you to see when you're running django locally and you start the server up this managed three don't do this later manage pi run server and then you talk to it and you will see all the get requests and so the django when you run it locally you can debug a lot of what's going on so you can see the you can see like the favicon that it's asking for and that is known five or six HTTP requests to produce that little Django web page and so that that will be something that you will learn how to do and so so that sums up what we were doing in this you know at the beginning you look at it very simply pretty soon you'll just be looking at the developer console and it'll all make sense to you I want you to be able to know that you can dig as deep as you want to and understand all of these protocols that are going back and forth okay Cheers [Music] [Applause] [Music]
Info
Channel: Chuck Severance
Views: 46,636
Rating: undefined out of 5
Keywords:
Id: PNt8zXl7EJ0
Channel Id: undefined
Length: 16min 7sec (967 seconds)
Published: Fri Sep 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.