Register for FREE and start learning Join today

Hide

This transcript is also available as a PDF download within the files tab.

Python 3 Compatibility

Now if you finish the exercise, you should have a function like this that returns the arguments that the user enters and the main argument or the only argument that it takes, it’s an argument for the target. Now I went one step further and I called the function in here and my main program exactly the way that I called it in the market changed your example when we did it in the previous section. So I just have a variable called options that will capture the value for options when get arguments is called. And then I’m passing options dot target to my scan function, which is the function that that does the scan and that takes an IP as an argument or as an input. So I ended up with a program that I can run like so I can just do python program name, which is network scanner and then I can do dash t or dash dash target.

And after that I can give the Ip that I want to scan. And in this example I’m going to give a range 10 zero two one over 24 to scan my whole sub net. And as you can see, I’ll get the results in here. Now this all works and it’s all good. The only thing is the opt parts module in here is deprecated. What we mean by deprecated is it still works. It even works with python three but if the python developers wanted to add new features, they’re not going to add it to this module. They’re going to add it to a different module that came after this. Now, the reason why I showed this module is because I’m actually very used on using it and I’ve been using it even before they created the new module and since this still works, I’m just used to always use this.

No, I’m not the only program where that does that. There are loads of programs and scripts online that’s still used the opt pars, hence why python made sure that it still works even in python three and hence why I told it to you because you might be looking at some code written by some other programmer and they might be used in opt fars. Now if you want to use the successor of [inaudible] powers, it’s called Eric Pars and I’m going to include it’s manual and the resources of this lecture but using it is actually very, very simple and very, very similar to use an opt parts and you’ll see why now. So in order to use our cars instead of important opt parts, we’re going to import arc powers. And then when you’re creating the parser here and instead of doing opt parts, obviously we’re going to say arc parts, which is the name of our module.

And instead of saying option Parser, the name of the class, in this case it’s called argument parser. Again, you can see the similarities. It’s identical. The only thing we’re doing is just changing the module name and the class name. Next, when you want to add an option and instead of saying dot add option, we’re going to say dot argument. Again, everything in here stays the same. We don’t need to change any of that. And then in here when we’re calling them parts arcs to parse the arguments, again, this is the same. We don’t need to change any of that. The only differences in this case, it only returns the options. It doesn’t return the arguments. So we can remove this in here and just return the options at the end. Now this code will work, so if we just go down and run it again, as you can see it’s still working and it works exactly the same.

The methods are still called exactly the same. The only differences, they’re called different names. And again, the only difference between this module and the opt pars is that opt parts is deprecated. So whenever python developers want to add new features, they’re going to be adding it to Arc Pars, not opt pars, but opt parts works with both python two and even python three so that’s very important to note. The other notes that I wanted to show in this lecture is using the whole script with python three so arc parts and of parts both work with python three so that wouldn’t be an issue. The issue would be with [inaudible]. Now it’s not really an issue, it’s just that you need to install KP on the system or in Cali to be specific because it doesn’t come preinstalled. So it comes pre installed with python 2.7 but it doesn’t come preinstalled with python three so this is a very good opportunity to me to show you how to install modules if they’re not installed.

So you can follow the method that I’m going to show you to install any module you want. It doesn’t have to be escaping. Now in order to install python modules, we use a command called pip. So you do pip install and then you put the module name that you want to install. So in our case it’s called escapee python three so pep is a command that you use to install python modules regardless of what module you want to install. So if you want to use a module in your program and this module doesn’t come pre installed with the operating system that you’re using, then you can use pip to install the module that you want. We’re using installed to tell pip that I want to install something because we can also use pip to do other stuff like removing modules. And finally the Po, the module that we want to install is called skp python three now this command will work to install modules for Python 2.7 if you want to install modules for python three then you have to say pip three so it’s the same way.

When we use python, we actually use python 2.7 and if we want to use python three we type python three same goes when you want to install modules. You do pip three if you want to install modules on python three you do pip if you want to install modules on python. So I’m going to hit enter and this will tell me that scape is already installed on my system because I actually already installed it before recording this lecture. But if it’s not installed on your system, then it’ll install it for you. Now once you have pip installed on python three you can actually use our code exactly the same way it is. So the program that we wrote, we’ll work on both python two and python three by default, you actually don’t need to modify anything. So if I just type python three year to run my program with python three you’ll see that the program will work exactly the same way that it used to work with Python 2.7 so that’s it for this lecture. I just wanted to give you two quick notes, and as we did this, we learned a few useful things.

  • Python 3 Compatibility

You must to access notes.

About the expert

Zaid Sabih

Zaid Sabih

Ethical Hacker, Pentester & Computer Scientist

See more from Zaid Sabih