Tell us what’s happening:
Hello Community, how you guys are doing?
So, i started the port scanner project related to Information Security Certification. I’ve written my project very similar to the one that is “given” in the youtube videos. So, i dont yet put in on test because i have a simple problem: Python. Anybody can help me?
The problem is with the input in the main function, how to make it work with my for? I thought using range but seems that not going to work…
import socket
def get_open_ports(target, port_range):
open_ports = []
scan = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for i in range(port_range, 1):
port=i;
def portScanner(port):
if s.connect_ex((target,port)):
print("The port ",port," is closed")
else:
print("The port ",port," is Open")
open_ports.push(port)
return(open_ports)
Using range function seems like a good choice. However notice that range doesn’t accept list as a parameter. It accepts from one to three numbers as separate parameters.
If you trust the port_range list is going to be 2 elements in length only, you can unpack values like so:
start, end = port_range
If you don’t, you can make it more explicit and take advantage reverse indexing (python feature):
start, end = port_range[0], port_range[-1]
In the first instance, if you use range you’ll need to add ‘+1’ to the ending value. In the second example, you can ... , port_range[-1] + 1
Hope it helps.