MongoDB with Async Python Transcripts
Chapter: Deployment
Lecture: Testing our Python Code in Production
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Very last thing, let's put it all together and make sure our web app or API or the thing
0:07
that is a client in Python talking to the MongoDB server in production can actually talk to the MongoDB server.
0:16
So remember, I created two servers, I created this one, this is the one we've been messing with. But I also added this Mongo course web app.
0:27
And that is where this code here is destined for. So let's go over there and just get it set up real quick.
0:35
So again, I changed my hosts file slash etc slash host to know that that goes to where its IP address is. And this thing is completely untouched.
0:46
It's a brand new Ubuntu 2022 10. Other than just running the apt update apt upgrade to make sure it's not out of date,
0:56
So what I want to do is somehow I want to get these files that we've been working on in GitHub over there. Well, Git is a pretty good way to do it.
1:07
So we're just going to come over, copy this. So we'll just say Git clone this. Now for you, you don't need a username or password.
1:14
It'll go right through because it's private until I release this course. I want to keep it basically hidden.
1:20
A decent way to do that is make it private. I mean, what does that word mean, right? It means make it hidden. So keep it hidden.
1:27
So I've got to put in my username and password here, but you won't need to do that, of course. There you go. It has cloned it.
1:36
To a great long directory there. Let's see if we got Python on this machine. Hey, Python 3.10. That's pretty good.
1:46
If you don't have it, you might have to apt install Python, but let's go into the code and we'll try to run this. It's not gonna go so great.
1:56
Let's just run the CLI one. Oh, we haven't, first we're gonna have to create a virtual environment for it, aren't we?
2:02
So we can have our things installed. Let's go back a little bit. I'll do it at the top here. Wait for requirements. Python 3-M.
2:11
VENV, VENV to create our virtual environment. Oh, we need to install a little more. I think the reason we actually had Python
2:21
is 'cause I installed Glances. So if you didn't install Glances, you have to install Python 3.10 and then this. Let's try again.
2:29
Oh, no, it's still going. Try again. Then we got to activate it. Notice our prompt changes.
2:41
And then we'll just Python, we'll say pip install -r requirements. That seemed to go well, so let's go into our code, chapter 11.
3:01
Now here is the error I expected to fail. Can we talk to localhost on 27017? Nope.
3:08
It's going to try for a while until it times out, and eventually we're going to get some kind of crash. So what is the problem?
3:17
Well, in our production version, we need to go and make it run with a different connection. So let's do that.
3:25
There's a bunch of different ways to do this. The part we want to see is in this startup here.
3:30
In this init connection, this is going to have to get more interesting, isn't it? Where are we going to put this information?
3:37
So I'll kind of simulate getting these values from the environment variables.
3:41
You would not put your username and password and stuff directly in here in a real app just
3:46
to keep it simple for you so you don't have to set up environment variables and all that.
3:51
But just for the simple version, dev mode, let's call it dev mode is true or false. I'll say down here if dev mode, we're just going to do this.
4:01
Otherwise, I'm going to do something way more complicated. Server I'm going to say server equals and remember this is in production, right?
4:11
it knows about let's go to the network. It knows about this IP address here. So when
4:28
we tell it the server, when and give it the server IP address, the port is 5621. All right,
4:37
a lot to pass over, but it should work. So if I run it locally right now it's in dev
4:42
mode. Looks like it works. And then let's just for production, we'll switch it over
4:50
again. That'd be an environment variable along with all the other things or a command line
4:55
flag or something. So we'll do a... I normally have been doing these get push details without
5:03
you all watching, but I got to do it now to get it up to the server, right? So over here,
5:09
you can see it crashed there once it finally timed out. All right, we're back. We should
5:18
be able to have, if we show you dev mode is false, so we should be running this code here. Let's give it a try. Check it out.
5:33
We got our initializing connection trace information and it's all set up. And look, it's working. It's working great.
5:42
Let's just do a few more things on the server for the fun of it. Let's see the most recently used packages. We create a new user.
5:51
All those things, right? Looks like we're talking to the database server perfectly. Excellent.
5:58
So while this might not look like a big deal, and you're like, ""Oh, we're just passing this
6:01
extra information over,"" just think about all the stuff that we've had to consider and put together to make this work.
6:09
We had the database before, but we set up a server, we configured MongoDB securely to
6:14
run on it, we put it on a non-default port, we gave it authentication, and we gave it
6:20
encryption, and now all within that virtual network, they're talking to each other, looking