Need hel with python for Port Scanner Challenge

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…

The input:

ports = port_scanner.get_open_ports("104.26.10.78", [8079, 8090])

Your code so far

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)

Challenge: Port Scanner

Link to the challenge:

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.

How can i change list to be accept in range?

You can unpack the list into the range. also remember to add one to the last value.

port_range[-1] = port_range[-1] + 1
for port in range(*port_range):
    print(port)
1 Like

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.

2 Likes

Nice! Thank you!
I`ve already solve the problem but thank you for spending your time helping me!

2 Likes