port scan

distrex distrex at hotmail.com
Wed Apr 10 17:29:37 EDT 2002


> 
> If by a port scanner you only want to scan for open ports, this is
> surely possible, and not difficult, either. Simply try to connect to the
> port, if this succeeds, you've found an open port. This is a minimal
> example for a Python portscanner (only checks for TCP so far):
> 
> #!/usr/bin/env python
> import sys, socket
> 
> def checkPortTCP(host, port):
>     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>     try:
>         sock.connect((host, port))
>         return 1
>     except socket.error, reason:
>         return 0
> 
> def main():
>     hostname = sys.argv[1]
> 
>     port_range = range(1024)
>     open_ports = []
>     for port in port_range:
>         if checkPortTCP(hostname, port):
>             open_ports.append(port)
>     print open_ports
> 
> if __name__ == "__main__":
>     main()
> 
> I recommend to look into the documentation of the socket module, and if
> this looks like chinese to you, as it was to me, the Socket Programming
> HOWTO at http://py-howto.sf.net/
> 
> Gerhard

Thanx for that it helped. That is what I wanted so I'll check out that
URL. Thanx again



More information about the Python-list mailing list