Cyber Class 9 Python Programming

Asked by GIANSH GUPTA KHUNDKER · BIRLA BRAINIACS

When writing a network scanning script in Python, which method should be used to try connecting to a remote port without crashing the program if the port is closed?

Answer as guest — Start Free Practice Test to earn Practice PointsHow to earn pointsAsk a question+3Answer a question+1Someone answers yours+1Your Q gets upvoted+1Pioneer bonus (first 5)+51 point = ₹1 discount on purchases, ask questions & climb the leaderboard!
Show answer and explanation

Answer: C. socket.connect_ex()

Answer: C) socket.connect_ex()
Explanation: While connect() raises an exception (like ConnectionRefusedError) if a port is closed, connect_ex() handles this gracefully. It returns an error indicator (like 0 for success or specific error codes like 111 for a closed port) instead of throwing an exception, making it perfect for smooth, efficient looping in port scanners.

Answering after opening this box earns no Practice Points.

70%