Writing a Python HTTP Server with Tornado (Explained with 4 Examples)


what’s up y’all this is Hussein Nasser
from IGeometry where we discuss software engineering by example in this
episode we will discuss how to build a Python web server a very simple Python
web server using tornado as the web server logic there so Before we jump
into that we’re gonna discuss what we exactly gonna build and I’ll show you
the examples we’re gonna show you four benefits of the web server so the web
server can be used to serve static files like a blog or or a static HTML page CSS
JavaScript could be used to build api’s right you can build your part you can
put your Python code at the backend and serve it with the HTTP protocol right
build API micro services all this cool stuff you can you can use it to build
proxy servers and other much much cool applications as well so with that let’s
just jump into it so first example here so we will show how to serve basic
content like so you’ll listen to the web server so this is a web server that is
running on tornado so you can just click there and then serve simple static text
right so I’m just returning hello world as a text another way of things is you
can use it to serve static content like I’m gonna serve my blog I’m gonna serve
an index dot HTML file and any HTML file that has CSS JavaScript and other stuff
so that’s another HTML file that I’m serving using the same web server
another thing is I want to use it to build an API so this is a simple API
that I he built to just tell you if a numbers an even or not so it’s not a
really complex stuff but it tells you the idea here so just as an example so
you can just you can see that I have a method here called is even you pass it a
query string that’s what’s called a query string
then you can pad as a number and then it tells you if it’s the number is odd or
even or whatever right so that’s well we’ll learn how to pull these query
strings from Python so that’s and the final way is to use same kind of an API
but it’s like cooler using the resource parameter so it’s like if you are
familiar with Twitter so you Twitter using the resource parameter like it’s
just like a tweak give me tweet number nine nine one two whatever right and
then you can use this ID to query a database and do cool stuff
YouTube use the same kind of string resources here alright let’s jump into
the actual coding what are you gonna need guys you’re gonna need Python
installed right and with that you’re gonna need pip installer and you’re
gonna need to install tornado so I have already installed tornado so I’m gonna
just show you how it to do it here tornado pepin install tornado says
already satisfied once you do that you are ready right so you go ahead with the
second step is to pick up your favorite text editor – Visual Studio code you can
use pycharm you can use any text editor that
understand Python I like Visual Studio code because it’s very light and it has
syntax highlighting it take care of all these it has debugging and so many cool
stuff as well so I like to use Visual Studio code here so I’m gonna go ahead
and open and you folder let’s create a new project here let’s call it by web
whatever and we’ll go ahead and open that what we need to do is create a
Python file let’s call it index dot pi and since we have an installed tornado
all we have to do is import tornado dot web and then we’re gonna import another
library that we’re gonna use tornado dot io loop so the web is we’re gonna
use it for the handler so guys if you remember the handlers are what we we
explained here so we have four hand drugs that we explained here okay all of
them actually pour into the abstract handler which is called a request
handler but there are different methods if you think about it okay where’s my
where’s my visual studio code where are you there you go okay so the i/o loop
will skip this is the loop that thread of keep listening to the request that is
coming essentially and it will be used to listen on a port okay
so with that uh I need to basically check if I am executing on the main right once I am executing on the main
what I want to do is create an app here and the app is totally do dot whit
oops tornado come on tornado that web that application okay so the application
here takes an array of what we call the request handlers so each request handler
is a tuple essentially so this is that let’s do the first one which is the hell
of work remember guys the hello world was just the slash like VI if someone
visit my route do that okay so let’s do this basic stuff
if someone visited my route I want to execute a request handler which was well
let’s call it the basic request handler how about that go and learn okay
and obviously this doesn’t exist right so we need to create that class right so
what we gonna do is basically inherit from tornado dot web dot request handler
okay and just like that we got an instance
occasionally a class which in Hertz from this request handler is the basic
request Handler and we’re gonna define a method get that takes a cell and what
that I get is actually the method that gets executed HTTP method right so you
can have get post options header so many methods right we’re interested in get
won’t leak for now so let’s go and write back hello word okay but we forgot to do
something very important here you’ll notice that I use a lot of semicolons
here because I’m used as I’m coming from a seashore a background in JavaScript so
just like at semicolon left and right so we forgot to actually listen to the app
so let’s listen to the port eight eight eight one wherever so to start the
thread we need to do tornado dot il loop dot il loop dot get the current thread
and then just go ahead and start it so that will start up our tornado app
essentially so but I want to print something that just to tell us like hey
I’m listening on port 8080 one you can just guys you can do it or not variable
and just do it that that’s the correct way of doing things so a basic request
handler let’s check that out we can even start debugging from here right and do –
that will tell us I’m listening on port 81 so let’s check that out eight eight
eight one there you go I remember this many banks that’s what I
did okay let’s stop that and let’s do another handler guys let’s do something
more fun let’s do the static handler which is the recovering an HTML
fire right let’s say I am creating an issue on fire here it’s called index dot
HTML and we’re gonna do like that’s the cool thing about visual studio code
yellow is just like to put boiler plate code if you if you guys didn’t catch
that just HTML colon 5 and just tap and then we’ll just do that so welcome to my
blog welcome to my blog guys and we just like I don’t know make it
hey there one write anything what we’re gonna do here guess what this is an
array just add another entry and says hey I want the blog entry to go to there
to this handler let’s call it static request handler okay and then you just
copy and paste if you don’t remember that stuff it’s called static handler
right instead of doing self that’s right we will do self de tren dere which is
essentially rendering a file that is in the same directory which is as the one
you read or running essentially indexes by index.html go ahead and render and
return this to the client okay all right let’s go ahead and start that maybe I’ll
just move this here all right so we have 881 as the route slash blog gives me
welcome to my blog guys all right so that’s the html5 we served cool cool
cool all right let’s do something more let’s do the function is even or as
prime right you can do any function here and we’re gonna do the query string now
so another request handler will be slash is even if someone visited their slash
is even handler we will call query string request Handler and then okay this doesn’t exist obviously what
we’re gonna do is steal from here finished and then we call this query
string request Handler and then guess what guys so now some you can we can get
that parameter or argument right remember guys because this is how we do
it right we gonna pass it as a query parameter using this question mark
parameter so we’re gonna do n does equal serve that get argument and let’s say
I’m assuming I’m getting argument and and I want n to be always an integer so
sure let’s do that and then well type in a result here that says it will are as
odd which is the result is odd F and modular – okay I think that will work
else even so that’s just a trend ternary operator there and we can say the number
that’s the cool cool thing about this the number n is is R which is whatever
it could be even or OD okay you get the idea guys right you can use this to do
whatever you want and refresh that’s the blog that’s the resource is even and is
equal eight eight is even it is even sir and you can put anywhere there and you
just take it we’ll take it guys I’m gonna put the github repo for this good
if you want guys and the description below so don’t worry it’s like you can
pause if you want I like to start from scratch all the time my own examples
okay final thing resource string which is a cooler cooler way of doing things I
guess right I did a video to do the different sick if you want to know the
differ between resource stirring and query
string or query parameter or resource parameter just click here I reference
list somewhere and the cards so you can just take a look at the video that I did
okay let’s do the tweet for example right or could be books you just like
you get the user will say tweet and then the ID so we’re expecting a number here
right right so you’re expecting a number and what you will pass is essentially
what do you call a regular expression here to expect telling telling this
thing that hey I’m gonna I’m gonna give you a number so expect a number and this
is how we do a number and regular expression so zero to nine and one or
more okay you do that and that is a resource let’s call it resource request
handler right watched or a resource resource handler is that thing let’s
copy from this guy the cool thing about this it’s slightly different for you
notice guys so this is gonna be slightly different this the parentheses will tell
this handler that hey expect another parameter let’s builders with the
resource handler okay expect another handler which is whatever we’re gonna
call it ID okay and then query Inc tweet num with ID ID so it’s like if you think
about it’s like a little cleaner I think that so you can just clearly see your
parameters here right instead of two using the get argument because you have
no idea what’s the schema kind of a thing right pure stuff but how about we
try this out guys how about we try this out let’s try this yet let’s give it a
short that’s even and then you do tweet / 9 right 8 right
so those example doesn’t seem very cool but it will allow you to do like a
boiler plate and actually do some work and like what you would do is initially
is like put the ID get it actually make a query to the tweeter Twitter API and
then get the information display to the user right you can do more cool stuff
alright guys that is all for me I hope you enjoy this episode check out the
other content of this canal subscribe subscribe to this channel I talk about
software engineering that’s my specialty here I talk about software engineering
by example that’s just one example of the code where we sit down and actually
do some code Python I tell Java Script c-sharp tutorials we have VB stuff we
talked about a lot of software interesting software engineering
concepts as well so check out that content if you if you enjoy it and I’m
gonna see you in the next one you guys stay awesome

Leave a Reply

Your email address will not be published. Required fields are marked *