How To Create a Simple Web Server Using Python and the http.server Module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back so today we're gonna be building a very simple web server using Python we're going to be using standard HTTP server module which comes shipped with Python when you download it so the first thing we can need to do in this video it's just download Python to your computer your machine so platform dot Hulk just under the latest version here once that's done we'll create I've created a web server folder on my desktop I'm gonna create a new file in this folder we call it web server dot Peyer okay now the first one we need to do is just import the HTTP server module so from HTTP server import so we want to classes from this module we want the HTTP server class and the base HTTP request handler class now we're going to use the HTTP server class to define our poll number to listen on as well as to serve the actual server forever until we press ctrl C to stop it and the base HTTP request handler class is going to handle all the get requests or post requests that the server receives and we're only going to be dealing with get requests in this video okay so now that it's done I'm just going to find a function called main and this is gonna be where we instantiate actual pull server name and we're going to serve the actual server so the first thing I'll do is I'll create a variable called port all uppercase called that will make it listen on port 8000 for now next thing what I do in this function is create a server so server equals and it's going to be an instance of the HTTP server class which we imported up here so HTTP server and this takes in two arguments reads the first finger takes in as a tuple which contains the hostname which we can leave blank in this case because it's going to be served up on localhost and it also takes the port number which we sated here support and in the second argument this HTTP server class takes is the request Handler so for now I'll just call this hello okay which we're going to create in a second next we want to do is just print to the terminal to say that the server's up and running so server running ports and we'll make that display to port number okay and then the last thing we're doing this function is serve out dot serve the surf forever method so as I said before this will start the server and it will run forever until we stop it with ctrl C in the terminal okay so that's the main function complete the next we want to do so is create a as we say it down here this hello hand the class want to create create this hello handler class here this is going to basically be used to handle all to get requests that the server receives so we'll start off by saying class hello and what this class does it inherits from this base HTTP request handler class so in base yes okay so the first time I do here we want to handle all get requests so we'll create a new method in this class define more statuses do get it takes in self as an argument okay so the first thing a service should do is send back a response so you know you have the status codes like the 200 which means you know that the files been found and it can serve the webpage so let's do that we do self dot send response in this case we can just put the status code in there which is 200 okay so the next thing the server does is it sends the will basically headers which details the content type which the web page will display so in this case I just want to do so send header I mean this case relates to content type in this case it's just text slash HTML that should be fine for now and then we always have to close the headers once we've listed them also I would do self dot and headers and that's fine like that okay so I also just want to set up some content to our page which we load when your server is running so for now if it's self dot W file which means writable file don't write this will write to the to the page in this case I'll just put some text so I and we also have to use the encode method here so what is encode method does basically for HTTP servers you cannot send strings on a HTTP request so this encode method basically basically encodes this string into bias and then it served up on the webpage as a string so that's basically it for now how always we now have to do is make sure this main function runs if we run this web server dot PI file directly from the terminal so in order to do that we do if name equals main and then we'll just make it run main yeah so basically that's saying if you know this if name equals main that means the file the web server don't PI files being run directly it's not being run as an imported module basically and this should be is so when we run our server and we visit put port 8000 we should see hello con I displayed in the web page so let's do that now I'll save my web server dot fire open up the terminal so I'm in my web server folder already we list you can see there's web set up higher file there so we'll do Python web server dot pi you can see we got ass right on port 8000 let's open that and you can see hello Connor if we change that starts over again hello world okay that's cool it's just to quickly finish off this and we're going to make this turn this into an echo server now say it's just for clarity we'll call this echo handler that's what the echo server does it's gonna take in the path the actual yeah the web address path and it's going to display it in the browser here so in order to do that we would do self dot right we'll just change this self dot right effort here and we'll go self to write so self dot path so the path takes in the the address path which is in the address bar up here so now if we run that see we've got hello we type something else in there and also if want to get rid of that forward slash we'll just slice the string the past ring so it goes from the the second the second if we put it at the second index it will take off this /bm forward slash here so if we save that refresh that and you can see it's removed the forward slash yeah this is basically it guys is a very simple web server I'm gonna expand on this same will you know going to post requests and you know credit applications so you know create and post Lee and post that kind of thing but this is just a very simple intro to the Python HTTP server module hope you got something like this video guys thanks for watching any questions please drop them drop me a comment below and I will answer as much as I can thanks gaying guys Cheers
Info
Channel: Conor Bailey
Views: 38,861
Rating: undefined out of 5
Keywords: python, web, server, http, module, get, request, class, method, function, http.server, simple, enccode
Id: kogOfxg1c_g
Channel Id: undefined
Length: 9min 53sec (593 seconds)
Published: Sun Jul 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.