Reputation: 11
I am learning to program in python3, I've been learning for a month, so I decided to try the python's socket module and make a port scanner.
import socket
host = input("Host: ")
port = int(input("Port: "))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, port))
except:
print(str(port) + " isnt open")
else:
print(str(port) + " is open")
The issue with the code is that the ports are never open, even the most common ones like 80 or 443. So I'm asking for you guys to help me fixing eventual bugs.
Upvotes: 0
Views: 486
Reputation: 21
I think another error is happening.
Have you tried printing the exception inside the except
block?
import socket
host = input("Host: ")
port = int(input("Port: "))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, port))
except Exception as err:
print(str(port) + " isnt open")
print(err.args) #for debugging
else:
print(str(port) + " is open")
This would let you see the actual error.
Upvotes: 1
Reputation: 15
Maybe this will help:
import socket
host = str(input("Host: "))
port = int(input("Port: "))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex((host,port))
if result == 0:
print('Port', port, 'is open')
else:
print('Port', port, 'is closed')
Upvotes: 0