how to build python web server ?


Warning: this is a virtual machine for learning process only not for production first, we must download install vagrant and virtual Box all links you need to create your first web server in the description also, source code this process is required to run the python server on your computer if you have already installed virtual machine Escape this step Download vagrant file with installed libraries and all configuration you need to Create Your First Web Server All links You will find them in description I will show webserver example restaurant Its Web server connected to database contains restaurants and menus Watch The Video And You Will able to complete and build it in days I will make you master webserver technique Now I read, create, update and Delete items in my Database also in my HTML template you see right now that’s called: CRUD operations Now Lets Start write codes We Import BaseHTTPRequestHandler and HTTP Server from BaseHTTPServer module We, Will, Use BaseHTTPRequestHandler to handle our GET and POST requests stop the video and write this import statement, please then we define our class let’s name it: WebServerHandler and give it BaseHTTPRequestHandler as an argument ! Now, If you think we are using this class to handle our GET & POST methods, then you are right now We have to define our main function or our server we will start filling the class and the main function so soon we write this if statement to execute the main function when we use python webserver.py that’s mean if name==main run this function main() let’s start with main() function our server I prefer to add the port in a variable we make it 8080 or any other port listed in the vagrant configuration file You can Check description for all files you need to build the app and run the virtual machine on your computer we define the server then add to it HTTPServer method which takes 3 parameters (host, port), and our class (WebServerHandler) leave host empty path your port variable to the functions as an argument The Important Part Let’s Copy Our Class Name And Pass it to the HTTPServer method print the message the server is running and pass the port variable to the print statement in a secure way using ‘%s’ % port we put them in a try block if it’s your first time deal with try and except no problem you can use if and else instead but if you like everything to be perfect like me so let’s use try and except in our code Now Run the server Using: serve.serve_forever() and it will literally serve forever I used to listen to a Music while coding now the server will serve forever how we will stop it !! now we use except or else KeyBoardInterrput Case which means the user has entered CTRL + C To Close The Server we will print message means the server will shutdown To shut down or close the connection to the server press CTRL + C but first use You have to add this method to your Server Server.socket.close() don’t forget server is the variable we have created in the try block we now finish our webserver it can run now but we need to define some output in the browser using GET Method let’s def do_GET method which handles get request pass self as an argument to it add try and except now now we check if the link is localhost:8080/home then connect to the user and send the response using method path.endswith(/home) or any other name of your page or directory How To send the response using the GET Method use .send_method(200) 200 is the response code for the GET Methods 301 for post we also will use .send_header() method to send the header to the user which contains the content type of our response which is text-Html then end_headers() to tell the browser you have finished your header finish the header the part I Was waiting for how to use HTML with python? how to host your HTML website with python we make variable name it output and add some HTML code add any HTML or css you need but OR JS !!! in this syntax output=” ”
output +=”

Hello World

” we add some style to the HTML codes you can use inline or internal style now let’s write our HTML code in the response page using self.wfile.write() method and pass the output variable to it and print the output for you to debug the code and fix the errors We use return to stop the function execution last line in our code except for IOError which mean if the user entered the wrong path or undefined path it will send the response with the error code 404 and the error message there is many errors types you can search we now learned how to path variable to our methods or even HTML output using %s and % variable name Now We Have Finished Our First Web Server Let’s Run it to see You can pause the video here to check errors or typo I’m using GitBash because I use windows if you have MAC or Linux use your terminal let’s give the file name webserver.py and save it in the vagrant directory you have Downloaded if you didn’t all links you need you will found them in the description give a name webserver.py How To Run or Start vagrant how to start the virtual machine first, we use cd and the path to our vagrant file and run vagrant up then vagrant ssh Please note it will take Many minutes
Especially if it the first time to run it now I finished running vagrant up and vagrant ssh let’s start our webserv using python last_webserver.py or whatever your server name webserver, My server, Dragon anything you need !oh there is an error prevent the server from running oh it’s typo error it must be server.serve_forever() let’s find it and fix it oh it was server.serve_forver not forever() now let’s run it Ha server response and tell us it running on port 8080 let’s check it localhost:8080/home here is your server respond with this HTML page You can Download The webserver.py check the video description Please Like And Subscribe Takes a long time to make it Thank You For Watching Good Luck & Keep Learning You Will be a Great Full Stack Developer