# Help with Python Port Scanner Please



## Ianvdl (Apr 8, 2009)

I started to learn python a few weeks ago and thought a port scanner would be a great way to learn a new language (string manip., networking, variables).

I even created a GUI for it using Tkinter, but I have two main problems:

1. The port scanner (Before I created the GUI) scanned until it found one open port, was supposed to close it, and the scan further. But, once it found an open port, it just flew through the others without scanning them, only displaying that it was trying to connect and went on to the next port. :sigh:

2. Finding the first problem (Not being able to solve it), I decided to try tkinter.
It all worked until I needed to get the IP to scan from the user through a text box.

After creating the text box and retrieving the IP from it, there always seems to be a new line string after it?! :4-dontkno This does not happen with the text boxes for the port to start and end.

Here is the full code of the port scanner as it is:


```
import socket
import Tkinter
print 'Modules Imported.'

win = Tkinter.Tk()
win.title('Port Scanner by Ianvdl')
win.geometry('400x180')
print 'Window Done.'

IP_Input = Tkinter.Text(win)
IP_Input.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
IP_Input.place_configure(width=120, height=20)
IP_Input.place_configure(x=40, y=10)
print 'IP_Input Done.'

PortS = Tkinter.Text(win)
PortS.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
PortS.place_configure(width=80, height=20)
PortS.place_configure(x=40, y=35)
print 'PortStart Done.'

PortE = Tkinter.Text(win)
PortE.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
PortE.place_configure(width=80, height=20)
PortE.place_configure(x=40, y=60)
print 'PortEnd Done.'

ScanButton = Tkinter.Button(win, text='Scan')
ScanButton.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
ScanButton.place_configure(x=10, y=150)
ScanButton.place_configure(width=120, height=20)
print 'Scanbutton Done.'

Label1 = Tkinter.Label(win, text='Percentage Done: Not Started.')
Label1.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
Label1.place_configure(x=10, y=95)
Label1.place_configure(width=200, height=20)
print 'Label1 Done.'

IP_Input.insert(0.0, '127.0.0.1')
print IP_Input.get(0.0, Tkinter.END) #Debugging

PortS.insert(0.0, '1')
PortE.insert(0.0, '65000')
print 'Load Done.'
IP_Input.focus()

################################################################

def Scan():
    print 'Scan Called.' #Debugging
    IP = str(IP_Input.get(0.0, Tkinter.END))
    print IP #Debugging
    Start = int(PortS.get(0.0, Tkinter.END))
    End = int(PortE.get(0.0, Tkinter.END))
    TestSocket = socket.socket()
    CurrentPort = Start
    OpenPorts = 0
    print 'Starting scan...'
    HowFar = int(CurrentPort/End * 100)
    ProgText = HowFar, r'%'
    Label1.config(text=('Percentage Done:', ProgText))
    
    while CurrentPort <= int(End):
        print 'Attempting Connection at:', IP, ':', CurrentPort
        print IP #Debugging
        print CurrentPort #Debugging
        try:
            TestSocket.connect((str(IP), CurrentPort)) #Test the connection
            print 'Open at:', IP, ':', CurrentPort
            TestSocket.close()
            print 'Closed the connection.'
            CurrentPort += 1 
            OpenPorts += 1
        except:
            CurrentPort += 1 
        finally:
            HowFar = int(CurrentPort/End * 100)
            ProgText = HowFar, r'%'
            Label1.config(text=ProgText)
    else:
        print 'Scan completed from port:', Start, 'to port', End
        print 'Amount of Ports open:', OpenPorts
        HowFar = int(CurrentPort/End * 100)
        ProgText = HowFar, r'%'
        Label1.config(text=ProgText)

################################################################

ScanButton = Tkinter.Button(win, text='Scan', command=Scan)
ScanButton.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
ScanButton.place_configure(x=10, y=150)
ScanButton.place_configure(width=120, height=20)

Tkinter.mainloop()
```
I know the code is very sloppy, and I should probably use threading to scan, but I have just started with Python.

Any help will be appreciated. :wink:


----------



## Squashman (Apr 14, 2005)

The best port scanner in the world I believe was written in Python. NMAP. You can download the program and I believe it comes with the source code.


----------



## Ianvdl (Apr 8, 2009)

Thank you for the reply.

I have heard of NMAP before, but I think it might be a little difficult to master the source code.

But I'll check it out anyway. ;D


----------



## Ianvdl (Apr 8, 2009)

Thank you for the reply.

I have heard of NMAP before, but I think it might be a little difficult to master the source code.

But I'll check it out anyway. ;D


----------

