 Welcome back, everyone. This is Brian. We're going to continue our journey into Python three. In this video, we're going to determine if a port is available. In the past few videos, we've kind of used a port number and we haven't really cared if something else is used it. So you may have actually crashed your script and had to have switched over to a different port. There are thousands of different ways of doing this, but I'm going to show you one simple easy technique that everyone can understand using the information we've learned in these videos so far. And you may have to do some creative Googling. I'm on Linux, but of course, Python runs just about everywhere. So you may have to Google how to find ports and use for whatever your OS is, whether it's Mac or Windows or whatever you're using. For Linux, I'm going to use the LSOF command. And it's going to return something like this, which is going to say a process along with who's running it, whether it's IP version for IP version six. And then it's going to tell us TCP because we're listening. And we want to know the port. The main takeaway from this is let's say I wanted to open something on port 631. I can't because CUPS is running on that. It's listening. There's an actual TCP server listening on that port. So if I go to use that port, it's going to, well, crash my program. The first step in any program is of course to import. So I'm just going to copy and paste. And we've got logging and socket. And then I'm going to configure logging. That's right. It's really that simple. We're going to keep this nice and short for this video. I want it dead simple to understand. All right, the bulk of this video here, we're going to check one port, we are going to later on in the next section, check an entire range of ports, but we're going to reuse a function over and over again. So we're going to make this function. So it's going to be called check underscore port. And initially, I'm just going to do a pass. And I'm going to say IP, port, and I'm out. And I'm not a professional typer. So you're going to watch me make a lot of mistakes. I apologize in advance. I'm only human. I always get envious to these guys that can type and talk and do all these great things at once. I am not one of those people. So we're gonna say logging debug. And we just want to put some debug messages just so we know exactly what we're doing here. I've actually heard from a friend of mine that they don't actually type most of the time. They actually have a program that's typing for them. So I thought maybe that would be a good Python project for me would be a auto typer so I could talk while it's typing for me. But alas, we haven't done that. So we're just going to watch me do my horrible typing. Now we're going to do a try block here. And we want try except finally. And let's go ahead and make this as exception as ex. Now I'm going to do this a little bit backwards here. The main goal is we want a value to be returned. So no matter what we do, we're going to return this value. So our immediate goal is to set this value based off what's going to happen. So we want to return a true or false whether or not the port is well in use. So let's go ahead and figure this out. Again, I'm going to do this a little bit backwards here. I'm going to do the finally first. So let's grab this. And I want to say returning IP port equals. And we don't want to know what value we're actually returning. And I'm going to grab this. Let's say error. And then I want to know what kind of error actually happened. Now we can if we really want to just in case we are super, super worried, we can actually set that defaults right there just in case. Now let's look at the bulk of our code here. We're going to say s equal socket dot socket. And this is going to be a socket in the address family of INET or IP version four. And I want to say socket and we're going to use a sock stream. Sock stream is a representation of TCP. So now that we've got that, I'm going to say socket dot that default timeouts, we're going to set the default timeout for our sockets. Now if we wanted to, we could actually do it on the individual socket itself, set timeout. I'm going to show you both ways of doing that. So comment that out just for giggles right now. Now what this does is it says if it's having trouble connecting, it's just going to immediately timeout after a given time frame and it's going to say nope, sorry, couldn't do it. Now from here, we're going to say we want a connection value. This is not an actual connection. And I want to say s dot. So we're saying socket connect underscore EX. You notice there's two different connects. There's connect and connect itself. We've used this before. This will connect to a remote host. We want connect EX. And what connect EX will do is it will give us an integer back, not an actual socket. It's going to say, hey, here's the end result of that connection attempt. It's not going to break our program if it can't connect is what I'm getting at here. It's going to give us an integer back saying yes, I could or no, I couldn't. All right, so we need an address. So I'm going to just make a tuple. And I'm going to say IP comma port. Now that we've got that, I want to know what the end result of that was. So I'm just going to grab this. And let's just print out the connection status. And again, this is going to be an integer of some kind of number representing what actually happened. Now we do have the socket lingering here. So we want to go ahead and close this. Again, this is all wrapped into a tri block just in case something goes boom. Now I want to say if on equals zero, that means, you know, sorry, couldn't do it. And I'm going to just for the sake of speed here, do the little bit of copying paste on my notes, and speed this up. It's pretty self explanatory what's going on. All right, so if we get a zero back, that means in use, we get something other than zero, it is usable seems pretty straightforward. Let's go ahead and flip into this. And we are going to now check a range. Okay, we're going to take this function, we just wrote check for and we're going to wrap it into a range of ports. So instead of checking just one port, I want to be able to check an entire range of ports. So we're going to reuse that function. So we're going to say def check underscore range. And I want the IP and a scope. And there are, again, thousands of different ways of doing this, I'm just showing you one way. So if you go to Google, you're going to find millions and millions of different answers on how to do this. I must say four P in whatever that scope is. And that if you're wondering what scope is going to be, it's just going to be a range object. Now in here, I'm going to say the result equals, and we're going to reuse our checkport function, which we just wrote, we need an IP, a port and a timeout. So I got the IP, we're going to grab that individual port. And let's go ahead and give it a timeout of one second. From here, I want to build up our dictionary. So I'm going to say ret. And we want to know that port, add a specific result. And then let's go ahead and return our dictionary object. So what this is going to do here is we're going to give it a range, let's say we can do like between port 1000 and 2000. And it's going to go through each one of those and check each individual port and see if you can connect to it. So if you can connect to it, it's in use, meaning another program is using it, and you don't want to try to use it. So we're going to say no, you cannot use that. However, if it's usable, we got an error trying to connect to it, that means there's nothing listening to it, then we can actually go out and try to bind to that. Now there are other things as well, but basic, this is really fundamental. We just want to know, can we attempt to use it doesn't mean we're guaranteed to use it, it just means we don't think beyond a reasonable doubt that there's something else out there using it. And check range is just going to go through a range of port numbers and check all of them at once. All right, the part that I often get excited for, let's go ahead and see it in action here. So we're going to make our main function. I always screw this up because I get so excited. All right, so let's go ahead and test one port. We're just going to try this out. So I'm gonna say P equals. And I want to check port. And let's check our local host, which again is just our local computer. We're not going out to the actual internet to do this. And we're going to check, I'm just going to make up a port 2594, why not? And we're going to say, you know what, I want a timeout of 2.0. Could have just said two, doesn't really matter. But point being, we're going to go to the local host and we're going to check this port 2594. I have not a clue if anything's even running on it. Actually, we can pull up our command line. I don't think it's on there 2594. I'm not seeing anything. So this should actually say it is usable. Let's test it out and find out here. So I'm going to say logging.info. And let's format this. And I want ports. What did we say what did we say here 2594 usable? And then we just want to know that end result there. So let's go ahead and test this out. And it's going to say port 2594 usable true. So reasonable suspicion, we can conclude that we should be able to use that port. Now let's go ahead and do the same thing. But instead of one port, I want to do a range of ports, uppercase that for consistency. So somebody doesn't yell at me, I get some squarely, squarely comments sometimes where somebody's like, I hate the way you type or I love the way you type. And I'm like, I'm sorry, I just type. All right, so we're gonna say local host. And we want to do a range. And let's pick a good range here. So here we've got some ports, we know are in use, right? So like 53. I like this 3306, which is my sequel, we got some Dropbox. So let's, let's pick on this little guy here 3306. So we're gonna say copy that. And let's go. I know that ports in use. So let's go to nine, just get a very short range here. This will return back a dictionary object, go ahead and clear that out while I'm thinking about it. And then from here, we're gonna say for e comma value in ports, dot items. And we're gonna say logging info. And I want to say port, whatever the key is, and we'll say usable, almost just, and it took the easy way and put an equal sign there. All right, so we're gonna go through that whole range there. Now we're doing a very short range just for demonstration, you could definitely do the entire machine if you want you make it access denied some other things depending on your operating system. So you got to be a little bit careful when you try this. Go ahead. Uh oh. Ah, see from or I told you I was gonna make a mistake. There we go. So we can see that 302 and let's scroll up here. So it starts at 00 usable true true true and ah, 3306 usable false. Let's pull this up, see what's on 3306. And you got it. It is my sequel. So that is really kind of the main takeaway here is that you can really quickly determine if a port is usable. And you can even check on a range of ports. Now, in the real world, honestly, you don't do this very often. You don't do this because you're going to let the user define what port or what port range they're going to work on. It's not really up to you to say I'm going to go find a free port. Now there are some practical applications of this. For example, if you have some graphical interface, and the user keys in what port they want and they hit enter, you want to go out and check and you know, be a little nice to the user and say, I'm sorry, something else is using that we need to pick another number. That's really the main takeaway out of this video. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers. And we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there, drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.