Welcome back.
It's time to use Python and build our real server.
Now.
What should we do here first?
Your first intuition might say, Hmm, I wonder if Python has a module already for that.
Well.
If you actually google python http server.
You see that?
Oh, look at that.
Python seems to have an HTTP server.
Awesome.
And this module actually allows us to create a server.
You can see over here that all we need to do is use the HTTP server and well, we have ourselves a server
and can actually read through the documentation and see how you can build one.
You can see over here, for example, that it's a simple couple of lines to create a server.
However, if I scroll all the way up, there is a warning here.
A HTTP server is not recommended for production.
It only implements basic security checks.
You see building servers?
Is a very, very common thing.
A lot of Python developers are hired as backend engineers to build things like servers.
So when we use something like the HTTP server that's built in as part of the Python standard library,
well, we're redoing all these things that, frankly, a lot of developers have done before.
And when you have a problem that is constantly getting solved or the problem is constantly being rewritten
and rewritten and rewritten, we start to have something called libraries, but also something called
frameworks.
And that is, instead of using something that, well, frankly, would take us a really long time to
do.
And as you can see here, there's still a lot of issues like security issues that we need to check and
fix.
We can use a framework like FLASK to build a server because so many engineers are building servers using
Python.
Flask is a tool that uses perhaps underneath the hood something like HTTP server, but instead make
sure that the security, the added tools and benefits are already pre-built for us.
You can think of it as a kitchen.
You're trying to bake a cake, and instead of you having to go by the knife, go by the bowl, the mixer,
the oven, go by the ingredients, all that stuff.
Flask is like the kitchen where you get to enter the kitchen.
You have all the tools necessary, you have the ingredients, and all you have to do is cook.
Now, when it comes to Python, there's two really popular frameworks.
There's the FLASK framework and the Django framework.
Right over here.
Now, Django is one of those frameworks that it's really, really big.
It's a big, big kitchen.
On the other hand, Flask is what we call a micro framework.
That is it's extremely lean.
It's a small library so that we can do things fast.
I'm going to teach Flask over the next couple of videos because I like the simplicity of it all.
Sometimes when you write in Django, it feels like you're not even writing Python because there are
so many rules and so many tools that you can use versus with flask.
Everything is clean and small and it's great for our use.
So how do we get started?
Well, as you know by now, as with any tool that we use, we should read the documentation.
Right?
And in our case, we can just go to installation and actually read through and see what they recommend.
You see over here that they recommend we use a virtual environment, something that we've already talked
about in previous lesson, and you'll see why this is important when we actually deploy the project
to production.
But it looks like we first create an environment.
So we make a directory project directory which hopefully we already have, and then we create a virtual
environment.
Now, one thing I haven't told you is that if you use Python three, which if you're following this
course, you already are using.
It comes prebuilt with something called V and V.
So instead of pip av or Virtual AV that I've talked about in the previous video on virtual environments,
this comes prebuilt, so we can actually just run this command.
In our terminal.
So let's give it a go.
And look at that.
A new virtual environment folder was just created.
Now, you see over here that the virtual environment was created well in a new folder called the V and
V.
If we actually leave out the first part or the last V and V or actually let's go back one directory.
And simply say.
Python three, dash m, v and V and then say web server.
The Web server now becomes the virtual environment because we already have the folder built.
So we don't want a new folder called Virtual Environment.
We can just delete that.
And instead have the virtual environment right over here.
Now, it also gives you the option on how to do it on windows.
So again, on Windows, you might have to run PI three.
Or if you're using Python two and you don't have the V and V module in that case while you have to use
virtual environment.
And then finally we have to activate the environment.
And all we need to do if you're on a mac or Linux is just grab this code or on Windows, grab this.
So let's just copy and paste this.
If I paste it in here, let's talk about what it's doing here.
We have to change this to where our folder is.
In our case, we're doing the web server, right?
So I'm going to say web backslash, space server.
Because well, that's the folder that we want to go into from desktop and inside of the bin folder.
Right here we have the activate executable.
So all we're saying is, hey, run this executable.
We don't need to worry about what it does.
But if I run this, we get a bit of an error.
Now, the issue here is that I'm using something called a fish shell.
And the reason I'm showing you this is because everybody's machine is different.
So whether you are on Windows or Mac or Linux or maybe you're using a different type of terminal, you
might get these errors.
Now the best way to solve this is if you go to the v.
V.
Python module and V scroll all the way down.
You'll see here that you can activate using certain scripts, but depends on which shell you're using.
Most of you are using PowerShell and Windows or Bash.
If you're using a mac.
In my case, I'm using this.
Fish.
So all I need to do is actually add this dot fish at the end of my script.
You'll notice here that you might have to add different things based on what you're using.
So if you're on windows, you're most likely going to add dot, bat or dot PS one.
So let's try that again.
I'm going to run the same thing, but this time say dot fish.
There you go.
It's now working.
And I am in the web server, which is this folder in the virtual environment.
So now I can install packages here and just be in my own little world.
Now, if for some reason activating doesn't work for you, you can actually still keep going with the
lectures.
It won't really affect you.
This is just one of those things that it's a little tricky.
You might not get it straight away, but the best thing to do is to always read the documentation and
follow the instructions.
All right.
That took an unnecessarily long time.
So let's finish off by installing Flask.
If we go back to our guide, we can finally install Flask by doing pip install flask.
Now again, we want to make sure that our PIP if we do version.
It's for Python 3.7, but for some of you, you might have to use PIP three.
So based on your needs, make sure you use the appropriate PIP installer.
Let's clear this up and just copy and paste and install flask.
Awesome.
We've just installed flask so we can start using it in our project.
But that was a whole lot of setup and a lot of downloading.
So let's take a break and I'll see you in the next video.
Bye bye.