The Python Bible Volume 2: Python Programming For Intermediates (Advanced, Professional) by Dedov Florian

The Python Bible Volume 2: Python Programming For Intermediates (Advanced, Professional) by Dedov Florian

Author:Dedov, Florian [Dedov, Florian]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2019-06-23T16:00:00+00:00


import socket

target = "10.0.0.5"

def portscan(port):

try :

s = socket.socket(socket.AF_INET,

socket.SOCK_STREAM)

conn = s.connect((target, port))

return True

except :

return False

for x in range ( 1 , 501 ):

if (portscan(x)):

print ( "Port {} is open!" .format(x))

else :

print ( "Port {} is closed!" .format(x))

So this scanner is quite simple. We define a target address. In this case, this is 10.0.0.5 . Our function portscan simply tries to connect to a certain port at that host. If it succeeds, the function returns True . If we get an error or an exception, it returns False .

This is as simple as a port scan can get. We then use a for loop to scan the first 500 ports and we always print if the port is open or closed.

Just choose a target address and run this script. You will see that it works.

Port 21 is closed!

Port 22 is open!

Port 23 is closed!

Port 24 is closed!

Port 25 is open!

But you will also notice that it is extremely slow. That’s because we serially scan one port after the other. And I think we have already learned how to handle that.

Threaded Port Scanner

In order to speed up the scanning process, we are going to use multithreading . And to make sure that every port gets scanned and also that no port is scanned twice, we will use queues.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.