Need help with port scanner

Iv watched all the videos and step by step but still having some issues

Your code so far

import socket

def get_open_ports(target, port_range):
    open_ports = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
start, end = port_range[0], port_range[-1] 

def PortScanner(port):
    if s.connect_ex((target, port)):
         print("The port is closed")
    else:
         print("The port is opened")  

return(open_ports)   

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68.

Challenge: Port Scanner

Link to the challenge:

1 Like

Tell us what’s happening. With what exactly do you need help? What did you try already?

1 Like
python main.py
Traceback (most recent call last):
 File "main.py", line 2, in <module>
   import port_scanner
 File "/home/runner/boilerplate-port-scanner-3/port_scanner.py", line 9
   if s.connect_ex((target, port)):
   ^
IndentationError: expected an indented block
1 Like

Well yeah, you either have to seperate values with commata in the print() or combine strings via +…
That’s a basic syntax error right there.

1 Like

Please do not take pictures of your screen. Please copy-paste code and errors into forum posts.

Thanks

1 Like

my apologies , we have had our power on and off for the past couple weeks since the winter storm that hit so access to my desktop is on and off as well .

1 Like

Totally understandable. I just mention it because not everyone realizes that pictures of code can be really hard to read.

1 Like

thank you , I edited the above to show the error .

1 Like

At least in this part, I think the unindented return is confusing python.

1 Like

okay , the return is what already filled when started so I just assumed that I had to keep it there. should I remove it ?

1 Like

It depends? I’m not sure which function the return is supposed to belong to. In python, indentation has syntatic meaning, which is to say that there are logic consequences to how code runs based on where the white space is.

import socket

def get_open_ports(target, port_range):
    open_ports = [] # open_ports defined here
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
start, end = port_range[0], port_range[-1] 

def PortScanner(port):
    if s.connect_ex((target, port)):
         print("The port is closed")
    else:
         print("The port is opened")  

return(open_ports) # so I'd expect this to be at the same indentation level

so maybe this?

import socket

def get_open_ports(target, port_range):
    open_ports = [] # open_ports defined here
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    start, end = port_range[0], port_range[-1] 

    def PortScanner(port):
        if s.connect_ex((target, port)):
             print("The port is closed")
        else:
             print("The port is opened")  

    return(open_ports) # indented like this maybe?

thank you so much , I tried that and it gave me another err. Iv tried changing the wording around ,

python main.py
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    import port_scanner
  File "/home/runner/boilerplate-port-scanner-3/port_scanner.py", line 10
    print(“The port is closed”)
             ^
"print(“The port is closed”)"
1 Like

It looks like those might be different quotation marks

“The port is closed”
vs
"The port is closed"

1 Like

oh good eye ! yes might have been when I tried doing it from my phone , ill try again and change it .

1 Like

okay i changed that
now getting this :

python main.py
Open ports: None
Open ports: None
Traceback (most recent call last):
File “main.py”, line 14, in
ports = port_scanner.get_open_ports(“104.26.10.78”, [440, 450], True)
TypeError: get_open_ports() takes 2 positional arguments but 3 were given

1 Like

It looks like you’re missing an optional third argument

The get_open_ports function should also take an optional third argument of True to indicate “Verbose” mode. If this is set to true, the function shourd return a descriptive string instead of a list of ports.

1 Like

like this ?
also I really appreciate your time and help . I get it down when I’m not doing a project , its just when I’m doing a project I forget everything

import socket

def get_open_ports(target, port_range, verbose=True ):
open_range =
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
start, end = port_range[0], port_range[-1]

def PortScanner(port):
    if s.connect_ex==(port + targer):
         print("This port is opened")
    else:
         print("The port is not opened")