nerdexam
Cisco

350-201(NEW-127Q) · Question #40

Refer to the exhibit. Which lines must be added to the script to list the ports that are open?

The correct answer is D. if result == 0: print "Port {}: Open".format(port) sock.close(). D is correct because Python's socket.connect_ex() returns 0 on a successful connection, meaning the port is open - so if result == 0 is the right condition. print is the valid Python output function, and sock.close() correctly closes the socket object using the proper…

Network Security

Question

Refer to the exhibit. Which lines must be added to the script to list the ports that are open?

Options

  • Aif result == 0: show "Port {}: Open".format(port) close()
  • Bif output == 0: print "Port {}: Open".format(port) open()
  • Cif output != 0: print "Port {}: Open".format(port) sock.open()
  • Dif result == 0: print "Port {}: Open".format(port) sock.close()

How the community answered

(29 responses)
  • A
    7% (2)
  • B
    3% (1)
  • C
    17% (5)
  • D
    72% (21)

Explanation

D is correct because Python's socket.connect_ex() returns 0 on a successful connection, meaning the port is open - so if result == 0 is the right condition. print is the valid Python output function, and sock.close() correctly closes the socket object using the proper object-method syntax.

Why the distractors fail:

  • A uses show instead of print (not a Python built-in) and close() without the socket object prefix - it should be sock.close().
  • B uses the wrong variable name output instead of result (the variable assigned from connect_ex()), and open() is a file operation in Python, not a socket method.
  • C also uses the wrong variable output and inverts the condition with != 0 - a non-zero return from connect_ex() means the port is closed or filtered, not open. sock.open() is also not a valid socket method.

Memory tip: Think of connect_ex() like Unix exit codes - 0 means success (port open), anything else means failure. Pair that with the pattern sock.close() (object dot method) to remember you always need the socket variable prefix when closing.

Topics

#Port Scanning#Socket Programming#Python Networking#Network Reconnaissance

Community Discussion

No community discussion yet for this question.

Full 350-201(NEW-127Q) Practice