Python comes with this cool little built-in server feature
called an HTTP server.
And it's not super useful for running a lot of Python scripts, but
it is super useful if you just want to run some sort of
static website from a particular directory.
So I'm on my desktop here, and let's say I have an HTML file
that I want to run. So I'm going to open up my editor,
and I'm going to create a new file,
and call this 'index.html',
'html:5'. Let's format this whole thing.
"Hello World".
And let's put an 'h1' in here.
"Hello World".
Now if I want to open this, I have to go to my browser,
go to 'File', 'Open File', and 'index.html', and you can see it has this
gross 'file:///Users', and it's the location to your file.
Now, if for whatever reason, this just does not work
well enough for you, maybe you're running a bunch of JavaScript
that actually needs a proper domain name,
what we can do is use an 'HTTP Server'. So we can simply type
'python3', or just 'python', because I'm already using Python3,
'python -m http.server', and this will run for me.
Just give it a second to boot up.
All right, so we can see that it's, Serving HTTP on 0.0.0.0.
That's our localhost on Port 8000 by default.
So let's go back to our browser.
I'm using Firefox.
Go to 'localhost:8000', and it renders,
and one's actually just zoomed in way too much.
But if I flip between these, they're the exact same.
The only difference is this one is now 'http://localhost:8000'.
And this one's just a file.
Now, where is this useful?
Actually, this is useful in a lot of cases.
One of the places that I use this most is in a project that
has documentation.
So whenever you clone down a project onto your computer,
it'll probably have some sort of instructions in there for
recreating the docs, like 'mkdocs', or it's going to be like 'make docs',
or something like that. Maybe there's a
script called 'docs', and then you just run
'docs', or maybe it's a Python file.
'python makedocs.py'
There's a lot of different ways to do it, but generally the
documentation comes in the form of a static website, HTML,
CSS, and JavaScript.
And when that happens, you can just 'cd' into your directory
through your command line, and run this.
Now, if this doesn't work, chances are it's because you have
Port 8000 already allocated, in which case we can cancel
with 'Control+C', and tell it to use a different Port.
So let's use Port 8123, and I just made that up.
It has no significance. And we just wait for it to boot up.
[no audio]
Let's see, 8000 does not work as expected.
And we go to Port 8123, and it works.
And once again, if you want to cancel that, just go to your
command line, hit 'Control + C', and it will cancel for you.
So that is a Python3 'http.server' built-in automatically to Python.
The one thing you have to make sure of is when you run this,
if I do 'ls -la' or 'dir' on Windows, you can actually see that
I've got an 'index.html' file in here, and this is where I
run the command.
[no audio]
Yes, I run it exactly where my 'index.html' file is, or wherever
the regular static files are.
That's the directory you're going to want to be in.
So get into your directory with 'cd',
whatever your directory is called, mine happens to be called
'Desktop', and then you can run the, 'python -m http.server'.
You could also pass it a Port if you want to.
8080 is a popular one.
8000 is probably the most popular one.
I've also seen 3000 and 5000 before.
Generally doesn't matter.
You just want some sort of Port that's not already used by
some program on your computer.