 Well, last week we were looking at setting up a very basic web server using Python, and one thing you couldn't do with that basic setup was have server-side scripts running. So today we're going to be looking at the options that are built into Python. So if you have Python on your system, you should be able to do this, that allow you to not only run a web server, but to run a web server with server-side scripts. So, first thing you do is you're going to have to create a CGI bin folder, unless you're going to change the configuration by default. All your server-side scripts should go inside a folder called CGI bin. If they're not in there, then they're not going to execute as a server-side script. So you would do make their CGI dash bin. I have already created one of those, and I've put a couple of scripts in there. Now, to start our server up, what we want to do is we want to start up Python, say dash M to load up the module, and we'll say CGI HTTP, and this is case-sensitive, capital S server. And also you want to tell it what port. Again, certain ports will be blocked by certain, by lower permissioned users. So port that your user is allowed to use, I'll just say 8,000 here. I'll hit enter, and you can see server HTTP on 0000 port 80. So it's running on this computer port, I'm sorry, port 8,000. Now if we look at our web browser, we can go to our web server. Again, this is on the local machine, so I can type in local host. I can type in 127.0.0.1. I can type in my local domain using the hostname.local, or I can put in my IP address. Of course, this is running locally. If you're going to be outside of the local network, you'll have to set up port forwarding. But I know that my IP address is 192.168.1.150 for this current computer. And as I said, port 80 is what we're using, so colon, sorry, port 8,000, so we're using colon 8,000. So anyone on the local network should be able to type that into their server, and they will get greeted with a directory list, luckily just like the simple HTTP server that Python uses. There is going to be a automatically generated directory list if there's no index file. And of course, there's a CGI bin. Now if I try to click on the CGI bin, it's going to give me an error. It's not going to give you a directory output, a directory list of what's in the CGI bin. This is one of the main security things that are put into place, because you don't necessarily want people viewing all your scripts, because certain scripts may, you basically just want to be careful with server-side scripts, because they're actually running on your machine. They can manipulate things not only as far as the web server goes, but could also manipulate things on your system. So you want to be very careful using server-side scripts. So it's going to require you to directly go to a script you created. Let me open up a new shell here and go into my CGI bin. And by the way, when you run the server, it's using the current directory as the root directory for your web server, unless you tell it otherwise. And again, the CGI bin is where your scripts are going to be. I've got a few in here, a few bash scripts, a couple of Python scripts. It does not matter what type of script or program it is, as long as your computer can run it and you've given it permission to run. So let's go ahead and have a look at this hello.sh. We say that it's a bash script, and we're going to do the output. Again, you're going to have to tell it that the first line has to be content type, and in this case, text slash HTML, and then you need to have an empty line after that before you start your code. And here we're just going to echo out something simple, but you could run any program that you want. So here's a very basic example. Again, if you're running this and for some reason you get this text output and not the actual script output, it's because you might have put an extra line in there. I'll give you an example of that. We have this file called hello.sh. You have to make it executable with changemod. I've already done that, but I'll show you changemod plus x and the name of your script. Running these on your web server is same as running it on your current computer. So if I was to run inside my shell here, hello.sh, you can see the output here. First line is this, then we have a blank line, and then our HTML code, which can be one line, but it can be a whole web page as well. So let's go ahead and run that up here. So to run that up here, all we have to do now that it's in there and made executable is again the address of the server, the port. If it's not port 80, if it's port 80, your browser will default to that. CGI bin and the name of the script. We'll go ahead and hit enter. And you can see we've got hello world. We can look at the code for that, which is just the text output there. I can go into that script again and I can add another line. I can say echo, this is a test. And I can give it more HTML tags if I want. I'm not going to here. I can save that. And if I was to close this and refresh this page, you can see now that text is right there. But again, we can also say give it a line break. Actually, we'll give it a horizontal line break there. And next, I can just run a command. If I wanted to list out the files in the current directory, I can use the list command. Now, do keep in mind that if you want line breaks and things such as that, you're going to have to implement the HTML code for that. So it will output the files in this folder, but not necessarily on a new line for each one. But let's go ahead and refresh that. And you can see the files in this folder. Or actually, it's giving the files in the main folder. I did not expect that. But just keep in mind that things aren't going to be on the new lines. You have to implement that. You'll put this through a while loop if you want. Should be able to do something like while read line, do echo dollar sign line. And we'll do a line break like that. And that should. Now, if I hit F5, put each thing out on a new line. If we look at the code, you can see that they're all on new lines with line breaks. Again, if we didn't have that, and we just had the list, I refresh the code up here, you can see everything's on a new line. But because we're writing our interface in HTML, you need to have those HTML codes for the new line. Very, very basic HTML. If you've worked with HTML before, you understand. This is very simple. I just wanted to point that out in case someone has never worked with HTML. OK, so that's a basic little script. We have a lot of echo lines there. Let's go ahead and look at another script that I called hello2.sh. And again, the extension.sh means absolutely nothing to the computer. Just as it means nothing when you're working in the shell, it means nothing on the web server. The extension means nothing. The most important thing is, of course, to have your shebang line, which is this first line here saying this is a bash script. If you don't have that, it's going to try to use the default shell, which is going to be a problem in some cases with bash scripts. If bash is not your default, but if you're using other programming languages like Python, which will get into a minute, that's definitely going to cost problems. Here's another example. I use this dash e here. So now I can put these new lines here. So this is the same original code that we have before. It's putting out this line of code. And then it's going line break, line break. So it has an empty line. And then it does our HTML code there. So let me go ahead, come up here. I'll hit two. So hello2.sh. And you can see that is the output of this file. So if you want to put all on one line, you can if it's something simple like that. Let's look at another example. Hello3.sh. And here you can see that I am using cat and EOF, although this doesn't have to be EOF. EOF stands for end of file. If you want to have more than one, you can label them different things. Basically what this is saying is everything between this EOF until there is a new line that has nothing but EOF on it. Print that out to the screen. So I'm doing the same thing here, the context type, content type, blank line, hello world. And then I'm also putting in a image here. So definitely if you're going to output a full HTML page, you don't want to echo out each line. You can cat it out like this. So here's an example. We can go three, hit Enter. And there we go. We have hello world and the image, which is the code down here. Now, things to be careful about. If I do this and try running this page again, you can see it outputs the text and doesn't output it as an HTML file because it doesn't realize it's an HTML file because the first line has to be this followed by a blank line for an HTML file. Otherwise, it thinks it's just a text file to display. And so if you put an extra blank line there like that or anything before that, it's going to not work right. So if you're going to be using the cat and a file, it might be a good idea to pretty much always start your files off like this. And that way, if we refresh, you can see it works. No matter what you change inside this main portion of the document, it's not going to screw that up. So that's another pointer for you. So we've worked with some bash scripts here. If I list out here in our CGI file, you can see that I have a couple of Python scripts. Again, the extension is for your knowledge, not the computer. It does not care what the extension is. It cares about the shebang line. And if I do within hello.py, you can see, I have a very basic I have my shebang line, which can be written in different ways. But as long as you have it and it works on your system, this is saying what type of environment to use, basically the interpreter, we're going to be using Python. And again, if you're using a different version of Python, all depends on how your server's set up. And again, same thing. I'm printing out the content type, a blank line, and then my code. So HTML code saying hello world as a header. So if I come in here and I change this to .py, hello.py, that's just again, because that's the name of the file, we'll hit enter, you can see it says hello world. Now I have another Python file in here called Python two, or hello two.py. Just to show you, similar to the cat end of file, we're using three quotation marks here and three quotation marks here. Everything between that is going to be outputted as text. So you don't have to do a different print line for each line of your HTML file, if you're going to be generating HTML file with your server side scripts. So again, I put my prints line for the content type and the blank line. These can be done on one line, but you want to kind of, I would advise doing that separate from the main portion of your webpage, just to prevent, again, if you accidentally put an extra line in there or an extra character or something, it won't run properly. So that's HTML, that's hello two.py, hit enter. And there you go, it's displaying an image, it says hello world. Now again, as I said, it does not matter whether it's a bash file, a Python script, a Perl script, and any type of script or any program that can run on your computer can be used as a server side script. And that's why you can use HTML as a GUI for any program. And I'm going to show you an example of that. If I go back into my main folder here. Oh, I also want to show you, if we go back into our main folder here, I have a Python script here that's executable. If I click that, you can see it displays it as text. It's not inside the CGI file, so it's going to run like that. So your server side scripts have to be inside that CGI bin file. Now I also have, as you can see here, a hello world file or a C code, which is a very basic, has a main function, prints out the content type and a new line. You can see I have two backslash ends there, that's a new line and a blank line, and I can print F hello world. So what I can do, I can come down here, I can say GCC, that's the compiler I use, I can say hello dot C, and I can say my output is CGI bin hello. And again, the extension doesn't matter. I'm just going to say dot bin, just so I know that that's a binary file. So I just compiled that. And if I was to actually cat that out to bin, so I cut out that file, you can see it is a binary file. It is a C file, it is compiled. So it doesn't matter whether it's a script or if it's a compiled program, if it's inside the CGI folder and it has executable writes, it can run. So if I come up here and I go back into my CGI bin folder, and I say forward slash hello dot bin, again the extension, the bin part means nothing, it's just letting me know that's an executable file. I can say dot exe, doesn't matter, it's on elix system, it's still compiled for Linux. And I'll hit enter and you can see it says hello from C because that's what the code was. And again, also the content type, dot html and that blank line, that's for the output. You don't even need that if you're just running the script. Let's say you're going to have some code that you can want to access through your web server and it does something on the server and you're not going to have any output. And that doesn't even really matter very much, although you pretty much always have some sort of output even if it's just script run complete or whatever. So I hope you enjoyed the tutorial. Please visit filmsbychrist.com. That's Chris with a K. There should be a link in the description. Again, this is using Python as a web server. Let's go ahead and look at that main script. Also you can see there was output here every time we access to file, just like when we ran servers before what files are being accessed. 200 means it was served up properly. And like if it's 404, you know it's not there. 403 is permissions issue, I think. That's what we were getting there. And again, the command is python-m and capital CGI-https-erver. So CGI-httpserver case sensitive and then what port you want to run it on. And when you run that whatever folder you're in, it will be running as that, as the root directory for your web server and all your scripts will go inside a CGI-bin folder. And also you may be asking yourself, let me go inside my CGI-bin folder. Here I'll create a new file. I'll call it who.... I don't even have to put it down. I can just say who again, the extension doesn't matter. And I'll say this is a bin-script and I'll just say uname-a. Save that. Again, I have to make it executable or it will not run. And you also want to consider permissions to write to stuff. You want to make sure that not everyone has permission to write to script files or even that folder because if someone somehow sees a flaw in one of your scripts to where it can redirect it and write a new file to your CGI-bin, well, it can create a new script that has the permissions as you're about to see. CGI-bin, again, we called that who with no extension hit enter. And oh, I forgot something. You can see there's no output even if I control you to look at the source code there because we forgot our content type. Remember if we cat out one of our hello.sh files, you can see, remember, don't forget this. I do that quite often. If you get no output, remember to have those lines. So we'll say content type, text.html, text slash html, then a blank line, and then our command, which can be anything we want. We'll hit F5 up here to refresh. And you can see that all the information about my server here and also actually that's about my server. I want to know about myself. So let me go back into my file here and I'll echo out a line break. And then I'll just say who am I? Save that, refresh it. And you can see it's running as MilX 1000. That's another thing to keep in mind that these scripts are running as whatever user you start the server as. So if you start the server as root, these scripts are gonna run as root. If you start them as a regular user, it's gonna have the permissions of that regular user, meaning those scripts can modify any scripts that user can run. So right now, if I write a script, it can modify anything in my home directory and the temp directory. Hopefully my regular user doesn't have permission outside of my home directory beside the temp directory. So keep that in mind. You may wanna create, especially, there's probably already a user on your system called, well, I don't know if it's by default. I know once you install something like Apache, it's there, but a www-data user that you may wanna start your scripts as that user, limiting their permissions. Unless maybe you're making a program that you want to be able to access and interface with certain user's files. Maybe you're making some sort of media center and you wanna be able to have permissions to start up mPlayer, because a regular web user shouldn't have permission to interface with your GUI. So all depending on what you're doing, just trying to bring that up to keep it in mind, security is important whenever you're doing server-side scripts, and especially, you know, you're opening this up to people on your network. So password protect stuff, be sure you have the right permissions on certain scripts and on certain folders. You don't want someone overwriting a script file with their own information that's going to be doing nasty stuff. That's just a little word of advice. Again, thank you for watching. Please visit FilmsByChris.com. That's Chris Decay. There should be a link in the description. I hope you're enjoying this series on servers and networks. And of course, next week we'll be continuing with a very basic look at another web server using Netcat, which you've probably seen before, but I'm gonna take it a step further than most people will show you. So I hope you look forward to that. And as always, I hope that you have a great day. Okay, this is an introduction to FilmsByChris.com. I'm Chris, that's Chris Decay. That's me right there. My daughter, Ember. And my wife, Jennifer. We pretty much live in the swamps of Florida. I'm a firefighter by day, as well as by night, we work long hours. But that's not why you're here. You're here about the videos I put up on YouTube. These videos are mainly about computers and programming, which means most of my videos look something like this. And if that's what you're interested in, great. If not, that's all right. I do videos on other topics too, such as video editing, special effects, photo editing, 3D design, and music creation. If you are one of my viewers and you enjoy my videos, my Patreon page is a place where you can go to help support my videos. So I ask that you take the time to go to my Patreon page and look at different levels of rewards you can receive for different levels of backing. There should be a link in the description of this video if you were watching it on YouTube. Otherwise, you can visit patreon.com forward slash metalx1000. And I thank you for your time and your support. Have a great day.