Modify the code below to display the information to the bottom of the page =====
ID: 3787254 • Letter: M
Question
Modify the code below to display the information to the bottom of the page
============================
#!/usr/bin/python
import socket
ipAddress = raw_input("Enter the target IP address: ")
portNumber = input("Enter the target port number: ")
socketAddress = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketAddress.settimeout(0.5)
if socketAddress.connect_ex((ipAddress, portNumber)):
print "Port", portNumber, "on the target", ipAddress, "is CLOSED"
else:
print "Port", portNumber, "on the target", ipAddress, "is OPEN"
====================
To show this info
=======================
Enter the target IP address: 192.168.121.56
Enter the target low port: 21
Enter the target high port: 26
Port 21 is open
Port 22 is open
Port 23 is closed
Port 24 is closed
Port 25 is open
Port 26 is closed
======================================
Include a solution both in python and in C++
Explanation / Answer
Please find the Solution in Python:
import socket
ipAddress = raw_input("Enter the target IP address: ")
lowport = input("Enter the target low port number: ")
highport = input("Enter the target high port number: ")
socketAddress = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketAddress.settimeout(0.5)
for i in range(lowport, highport+1):
if socketAddress.connect_ex((ipAddress, i)):
print "Port", i, "on the target", ipAddress, "is CLOSED"
else:
print "Port", i, "on the target", ipAddress, "is OPEN"
Sample output:
Enter the target IP address: 192.168.0.112
Enter the target low port number: 20
Enter the target high port number: 26
Port 20 on the target 192.168.0.112 is CLOSED
Port 21 on the target 192.168.0.112 is CLOSED
Port 22 on the target 192.168.0.112 is CLOSED
Port 23 on the target 192.168.0.112 is CLOSED
Port 24 on the target 192.168.0.112 is CLOSED
Port 25 on the target 192.168.0.112 is CLOSED
Port 26 on the target 192.168.0.112 is CLOSED
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.