 Okay, today we're going to be looking at setting up a web server very quickly with Python. Just default Python, what comes with Python. So if you have Python on your system, you should be good to go for setting up a quick web server. And this first part you've probably seen before, if you've ever Googled Python web server. It's the first thing that comes up. It's a simple HTTP server. So we're going to run Python, say dash and we're going to say simple and this is case sensitive HTTP server. So capital S, capital HTTP, capital S and then whatever port you want to run it on, so we'll say 800 here. And I'll hit enter and it says serving HTTP on 0000 port 80 or sorry, 8000. Now here's my web browser, let me refresh and look, you can see there are two files. Now one of the advantages to this over the default setup for busy box, HTTP which we've gone over previous weeks, is that if there's no index file, it is going to create a index file for you, a directory list for you. So you can see there's two files in the current directory, which by the way when I start up the script, it is running using the current folder. So I'm going to run this in the folder I want. And here's an example, here's a picture I can click on and there's just a little simple PNG I threw in there that says filmsbychrist.com, which is a great website, you should visit it and I can go back. Now let me control C a couple times down here in our shell window and kill that server. So now if I come back up here and hit F5 to refresh, you can see the web, this web page is not available because we shut down the server. So let's create a HTML file, so I'm going to vim and I'll call it hello.html and in here we'll create a very simple hello people, yeah, forward slash one. So just give it some header tags there. And now we'll start up our server again, hit F5 and now you can see that that file is there. And if I click on it, it displays it with the bold text because it's reading it as an HTML file. Now if I was to again kill our server here and move our hello.html to be called index.html and I was to go back here. It's still showing it when I hit back page even though the server is down just because it's cached in the web browser. If I hit F5 at this point, you can see that the web server is down. I'm going to start it up again running the same command, python-m simple HTTP server on port 80. And as you can see, my web browser automatically updated once it realized the server was up and it says hello people. The reason for that is if there's an index.html file, it's going to default to that rather than directory list. So now I can't view that list of files. If I know the name of one of those files, I can say forward slash. What did I name that image file? Well, let's list here. Oh, that's right, fbk for films by chris.png. So even though I can't get the directory list, I can say fbk.png and still view that file. But again, if I go back to just localhost 8000 here and hit enter, it defaults that index file. Also, I'm doing localhosts. I'm connecting back to myself. Other options, since I'm connecting back to myself, can be 127.0.0.1, which is the same as in most cases is doing localhost. So you can see that runs the same. Another option would be my current IP address on my local network. If someone else on the local network wants to connect now, if you're at a remote location, you're going to have to set up port forwarding on your router to access it. But locally I can say 192.168.1.150, port 8000, again, because I know that's the IP address of my system, and that will work. And another thing, if I kill here, this should work. You can see the name of my host right here is grml. I should be able to type in grml.local colon 8000, hit enter, and that didn't work just because I killed the server down here. So let's go ahead, restart the server and then refresh our page up here, and you can see it works. And so as long as your network is set up this way, and most networks will do this, and your computer is set the right way, and your browser knows how to resolve names, you can use grml.localhost. And again, grml is the name of my host on my system here. So when you're in your shell, whatever is past your at symbol, you should be able to access that computer while you're on that local network by using grml.local, or that name and dot local, and the port name there. So that's a few options there. But now let's say I want to only be able to have a loopback connection. Loopback connection means, well, let me kill our server here, let me maximize the screen here, clear it. If I type in if config, you can see I have three network cards. I got my wired ethernet card, you have a LAN card, and you also have this loopback device. This loopback connection is a virtual network device. You can have no other network cards in this, and you can have a loopback device, and that just allows your computer to connect to itself without sending anything out through the network. And you can use this, in many cases, it's not uncommon to see applications using this to allow a program to use network services without having to go out to the network. And that way you can avoid, you know, one other people connecting to those services and also prevent network sniffing from other devices, obviously, if someone's sniffing on this device and they're sniffing that device, there's not much you can do about it. You have many problems then. But let's say you wanted to set up a web interface with maybe a form to fill out that saves stuff to a database, and you want to use web services, but you only want this computer to be able to connect to it, you can use a loopback device. So how can you use this Python example to only access through loopback? Well, yeah, you're going to have to write a little script. It's a simple little script. You can Google this. I've made it as simple as possible, and I've put it on here. I've called it serverpy. And first line always with any script, you're going to want to use your shebang line to tell your computer what interpreter to use, if you're having issues and you come and ask me, can you look at my code and you don't have that, I'm going to yell at you. I don't care. People write it different ways and you can have different opinions on how you write it, but make sure you have that shebang line saying this is a Python script. Okay, next we're going to import a few modules, our sys module, and actually we don't need that sys module. That was from something, originally I was writing this and you could pass arguments to it, I decided not to do that because I decided to keep the script as simple as possible, so I don't think we need that sys module. So the only module we need to import is base HTTP server. And from that we're going to use the simple HTTP server, which is what we were just using, and from that we're going to import this handler request. Now we're going to set an object, a simple HTTP request handler, and set our server object to base HTTP server and dot HTTP server, and we're going to use those further down here. Next we're going to create a variable that contains two bits of information, the IP address, and the port. And since we're using this loop back IP address, it will be able to loop back but not use those external IP addresses that are connected to other network cards. Next what type of protocol version are we going to use, we're going to use HTTP one. And then we're going to create this HTTP object, it's going to be a server using, see this object up here, we're going to pass it some information, the server address which we've created up here with the port, and we're going to say use this handler, which handler, well this one right up here that we imported up here. We use it up here, in fact you can actually put this right in here, but it's just shortening it here because we write it here in there. Anyway, and then this line here just says continue forever, otherwise the script would run and close and no one would be able to connect. This says as long as the script is running, keep this process run, process running. So let me save that, you'll want to make it executable, change mod, server.py or wherever you call it, and we'll run it. I didn't set it to giving output by default when you started up, you can do that if you'd like. Now, if I hit F5 up here, hit enter, you can see web page not available. If I use the GRML local host, if I try 192.168.1.150, port 8000, same thing. But because those are using, in this case, my ETHO card because that's where I'm connected to the regular network with. So it's using my actual network card. But if I was to do local host, that works. And if I was to 127.0.0.1, that also works. So that will prevent anyone else on the network from being able to access this server. The only people who can access the server are the people that are on this machine. Yes, you can tunnel through with SSH, but then you're doing that on purpose. So yes, you can create applications and you can set them up so that only this computer has access to them. Now, if I was to kill this and let's remove that index of HTML and start that server up again and refresh up here. We're back to our directory list. Again, you can see we have our image file here and we also have this server py file, which is the process we're running here. If I was to click on that, you can see it displays, it doesn't run it. So can you create scripts that will run on the server side using Python? The answer is yes, and that's what we're gonna be looking into next week. Setting it up, basically the same type of service, but allowing scripts to run on the server end. And that will definitely help you for creating applications and you wanna be able to do more advanced stuff. You're gonna want stuff to run on the server and we'll go over that next week. I hope you enjoyed this tutorial. I hope you continue watching this series. I hope you subscribe. If you like these videos, be sure to like them, give them thumbs up, and be sure to share them. It really helps me when you share your videos, gets me more viewers, gets more interaction from people. So if you enjoy these and you wanna share them, please do so. And as always, visit filmsbychrist.com, that's Chris of the K. There should be a link in the description. And as always, have a great day.