I'm just starting to learn programming python, and have been following a tutorial for creating a simple port scanner in order to learn about programming sockets. I'm able to make a successful connection to localhost when I manually enter all the code for a single iteration, however if I take the same code, and apply it within a for loop utilizing try/except, I immediately get exceptions for every port in the range, even when I know that some of the ports are open. I believe that I've isolated the problem to socket.connect() because I've entered code below that that I know never gets executed.
I can enter the following code, and get a successful return:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
port = 22
s.connect(('127.0.0.1', port))
s.send(b'test')
banner = s.recv(1024)
print(banner)
s.close()
returns:
b'SSH-2.0-OpenSSH_6.2\r\n'
Process finished with exit code 0
However, as soon as I take that code and move it into a for loop with the port number as the iterator, it stops working.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
for port in range(1,26):
print("[+]Attempting to connect to : " + str(port))
try:
s.connect(('127.0.0.1', port))
s.send(b'test')
banner = s.recv(1024)
s.close()
if banner:
print("Port " + port + "is Open: " + banner)
except: print("[+]Port " + str(port) + " is closed")
returns:
[+]Attempting to connect to : 1
[+]Port 1 is closed
[+]Attempting to connect to : 2
[+]Port 2 is closed
[+]Attempting to connect to : 3
[+]Port 3 is closed
....ETC....ETC....ETC....
[+]Attempting to connect to : 24
[+]Port 24 is closed
[+]Attempting to connect to : 25
[+]Port 25 is closed
Even though I KNOW port 22 is open and listening on localhost. (ie I am able to ssh to 127.0.0.1 without issue). I have tried everything I can think of to no avail, including changing the data type of port to an int manually by using the internal int() function, I've tried the socket.connect_ex object, etc. I've also put code right below the socket.connect statement just to see if it shows up, which it never does.
The Zen of Python states:
Errors should never pass silently.
Unless explicitly silenced.
Only you have not silenced the error but instead just replaced it with a message that is non-descriptive of what actually happened:
>>> "Port" + 1
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
"Port "+1
TypeError: Can't convert 'int' object to str implicitly
is what you will get if opening port 1 worked, but after you close a socket you can't connect to anything else:
>>> a = socket.socket()
>>> a.close()
>>> a.connect(("www.python.com",80))
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
a.connect(("www.python.com",80))
OSError: [Errno 9] Bad file descriptor
So you need to create a new socket inside the loop for it to work properly but most importantly: you need to limit the errors you catch:
try:
#if this is the only line you expect to fail, then it is the only line in the try
s.connect(('127.0.0.1', port))
except ConnectionError:
#if a ConnectionError is the only one you expect, it is the only one you catch
print("[+]Port " + str(port) + " is closed")
else: #if there was no error
s.send(b'test')
banner = s.recv(1024)
s.close()
if banner:
print("Port " + port + "is Open: " + banner)
then you will see the actual errors you are getting instead of guessing what went wrong which is also against The Zen of Python :
In the face of ambiguity, refuse the temptation to guess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.