1 00:00:00,000 --> 00:00:02,820 Python comes with this cool little built-in server feature 2 00:00:02,820 --> 00:00:04,400 called an HTTP server. 3 00:00:04,400 --> 00:00:08,100 And it's not super useful for running a lot of Python scripts, but 4 00:00:08,100 --> 00:00:10,500 it is super useful if you just want to run some sort of 5 00:00:10,500 --> 00:00:13,200 static website from a particular directory. 6 00:00:13,200 --> 00:00:17,100 So I'm on my desktop here, and let's say I have an HTML file 7 00:00:17,200 --> 00:00:21,500 that I want to run. So I'm going to open up my editor, 8 00:00:21,500 --> 00:00:22,600 and I'm going to create a new file, 9 00:00:22,900 --> 00:00:26,300 and call this 'index.html', 10 00:00:28,000 --> 00:00:30,800 'html:5'. Let's format this whole thing. 11 00:00:30,800 --> 00:00:32,700 "Hello World". 12 00:00:32,700 --> 00:00:34,800 And let's put an 'h1' in here. 13 00:00:34,800 --> 00:00:36,100 "Hello World". 14 00:00:36,100 --> 00:00:39,100 Now if I want to open this, I have to go to my browser, 15 00:00:40,400 --> 00:00:49,000 go to 'File', 'Open File', and 'index.html', and you can see it has this 16 00:00:49,060 --> 00:00:56,740 gross 'file:///Users', and it's the location to your file. 17 00:00:57,040 --> 00:01:00,520 Now, if for whatever reason, this just does not work 18 00:01:00,530 --> 00:01:02,710 well enough for you, maybe you're running a bunch of JavaScript 19 00:01:02,710 --> 00:01:04,700 that actually needs a proper domain name, 20 00:01:04,700 --> 00:01:10,100 what we can do is use an 'HTTP Server'. So we can simply type 21 00:01:10,100 --> 00:01:15,800 'python3', or just 'python', because I'm already using Python3, 22 00:01:15,800 --> 00:01:22,300 'python -m http.server', and this will run for me. 23 00:01:22,300 --> 00:01:24,800 Just give it a second to boot up. 24 00:01:25,500 --> 00:01:29,500 All right, so we can see that it's, Serving HTTP on 0.0.0.0. 25 00:01:29,500 --> 00:01:33,000 That's our localhost on Port 8000 by default. 26 00:01:33,000 --> 00:01:35,100 So let's go back to our browser. 27 00:01:35,100 --> 00:01:36,100 I'm using Firefox. 28 00:01:36,100 --> 00:01:39,800 Go to 'localhost:8000', and it renders, 29 00:01:41,100 --> 00:01:43,400 and one's actually just zoomed in way too much. 30 00:01:43,580 --> 00:01:46,160 But if I flip between these, they're the exact same. 31 00:01:46,160 --> 00:01:50,600 The only difference is this one is now 'http://localhost:8000'. 32 00:01:50,600 --> 00:01:52,000 And this one's just a file. 33 00:01:52,040 --> 00:01:53,180 Now, where is this useful? 34 00:01:53,190 --> 00:01:55,340 Actually, this is useful in a lot of cases. 35 00:01:55,430 --> 00:01:58,460 One of the places that I use this most is in a project that 36 00:01:58,470 --> 00:01:59,450 has documentation. 37 00:01:59,810 --> 00:02:02,990 So whenever you clone down a project onto your computer, 38 00:02:03,170 --> 00:02:05,840 it'll probably have some sort of instructions in there for 39 00:02:05,840 --> 00:02:10,699 recreating the docs, like 'mkdocs', or it's going to be like 'make docs', 40 00:02:10,699 --> 00:02:11,800 or something like that. Maybe there's a 41 00:02:11,800 --> 00:02:15,100 script called 'docs', and then you just run 42 00:02:15,160 --> 00:02:16,680 'docs', or maybe it's a Python file. 43 00:02:16,680 --> 00:02:19,000 'python makedocs.py' 44 00:02:19,080 --> 00:02:21,570 There's a lot of different ways to do it, but generally the 45 00:02:21,570 --> 00:02:25,800 documentation comes in the form of a static website, HTML, 46 00:02:25,860 --> 00:02:27,360 CSS, and JavaScript. 47 00:02:27,370 --> 00:02:30,030 And when that happens, you can just 'cd' into your directory 48 00:02:30,030 --> 00:02:32,500 through your command line, and run this. 49 00:02:32,520 --> 00:02:35,190 Now, if this doesn't work, chances are it's because you have 50 00:02:35,200 --> 00:02:38,970 Port 8000 already allocated, in which case we can cancel 51 00:02:38,970 --> 00:02:42,230 with 'Control+C', and tell it to use a different Port. 52 00:02:42,240 --> 00:02:46,550 So let's use Port 8123, and I just made that up. 53 00:02:46,550 --> 00:02:49,600 It has no significance. And we just wait for it to boot up. 54 00:02:49,600 --> 00:02:53,100 [no audio] 55 00:02:53,100 --> 00:02:55,200 Let's see, 8000 does not work as expected. 56 00:02:55,200 --> 00:02:59,800 And we go to Port 8123, and it works. 57 00:02:59,800 --> 00:03:03,200 And once again, if you want to cancel that, just go to your 58 00:03:03,200 --> 00:03:07,000 command line, hit 'Control + C', and it will cancel for you. 59 00:03:08,100 --> 00:03:13,300 So that is a Python3 'http.server' built-in automatically to Python. 60 00:03:13,390 --> 00:03:16,380 The one thing you have to make sure of is when you run this, 61 00:03:16,900 --> 00:03:20,700 if I do 'ls -la' or 'dir' on Windows, you can actually see that 62 00:03:20,700 --> 00:03:24,000 I've got an 'index.html' file in here, and this is where I 63 00:03:24,000 --> 00:03:25,200 run the command. 64 00:03:25,200 --> 00:03:27,100 [no audio] 65 00:03:27,100 --> 00:03:30,800 Yes, I run it exactly where my 'index.html' file is, or wherever 66 00:03:30,800 --> 00:03:33,700 the regular static files are. 67 00:03:33,700 --> 00:03:35,900 That's the directory you're going to want to be in. 68 00:03:35,900 --> 00:03:38,500 So get into your directory with 'cd', 69 00:03:38,500 --> 00:03:40,400 whatever your directory is called, mine happens to be called 70 00:03:40,400 --> 00:03:46,200 'Desktop', and then you can run the, 'python -m http.server'. 71 00:03:46,220 --> 00:03:48,260 You could also pass it a Port if you want to. 72 00:03:48,270 --> 00:03:49,580 8080 is a popular one. 73 00:03:49,590 --> 00:03:51,710 8000 is probably the most popular one. 74 00:03:51,860 --> 00:03:56,940 I've also seen 3000 and 5000 before. 75 00:03:56,950 --> 00:03:58,200 Generally doesn't matter. 76 00:03:58,200 --> 00:04:00,900 You just want some sort of Port that's not already used by 77 00:04:00,900 --> 00:04:02,700 some program on your computer.