How to create a UDP port scanner in python 3?

1

My code is always giving "port is opened". My idea is: if the destination responds, the door is open. Otherwise, it may be filtered ...

#####################################
# Portscan UDP        #
# #
#####################################
# -*- coding: utf-8 -*-
#!/usr/bin/python3
import socket

ip = (input("Type IP or Address: "))

ports = []
count = 0

while count < 5:
    ports.append(int(input("Type the port: ")))
    count += 1


for port in ports:
    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    client.bind((ip, port))
    msg = 'hi'
    client.sendto(msg.encode(), (ip,port))
    data, address = client.recvfrom(1024)
    #print("Recebida ->", str(data))




    if data != None:
         print (str(port) + " -> Port is opened")
    else:
         print (str(port) + " -> Port is closed")

print ("Scan Finished")
    
asked by anonymous 18.08.2016 / 17:08

1 answer

2

See if this results:

import socket

ip = '127.0.0.1'
while True:
    port = input('port?\n')
    if(port == 'exit'): break
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((ip, int(port)))
        print('Port {} open'.format(port))
    except:
        print('Port {} not open'.format(port))
    s.close()
    
18.08.2016 / 19:25