简体   繁体   中英

Port Scanner python script

I'm a beginner to python and i'm learning the socket objects in python. I found out a script on the internet that is:

import socket

s = socket.socket()
socket.setdefaulttimeout(2)
try:
    s = s.connect(("IP_ADD", PORT_NUM))
    print "[+] connection successful"

except Exception, e:
    print "[+] Port closed"

I just wanted to ask, that whether this script can work as a port scanner? Thanks alot!

Just change your code, it can be used as a TCP port scanner for localhost :

import socket

def scan_port(port_num, host):
  s = socket.socket()
  socket.setdefaulttimeout(2)
  try:
      s = s.connect((host, port_num))
      print port_num, "[+] connection successful"

  except Exception, e:
      print port_num, "[+] Port closed"

host = 'localhost'

for i in xrange(1024):
  scan_port(i, host)

But it is just a toy, you can not use it for something real, if you want scan the ports of other's computer, try nmap .

Here is my version of your port scanner. I tried to explain how everything works in the comments.

#-*-coding:utf8;-*-
#qpy:3
#qpy:console

import socket
import os

# This is used to set a default timeout on socket
# objects.
DEFAULT_TIMEOUT = 0.5

# This is used for checking if a call to socket.connect_ex
# was successful.
SUCCESS = 0

def check_port(*host_port, timeout=DEFAULT_TIMEOUT):
    ''' Try to connect to a specified host on a specified port.
    If the connection takes longer then the TIMEOUT we set we assume
    the host is down. If the connection is a success we can safely assume
    the host is up and listing on port x. If the connection fails for any
    other reason we assume the host is down and the port is closed.'''

    # Create and configure the socket.
    sock = socket.socket()
    sock.settimeout(timeout)

    # the SO_REUSEADDR flag tells the kernel to reuse a local 
    # socket in TIME_WAIT state, without waiting for its natural
    # timeout to expire.
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # Like connect(address), but return an error indicator instead
    # of raising an exception for errors returned by the C-level connect() 
    # call (other problems, such as “host not found,” can still raise exceptions). 
    # The error indicator is 0 if the operation succeeded, otherwise the value of 
    # the errnovariable. This is useful to support, for example, asynchronous connects.
    connected = sock.connect_ex(host_port) is SUCCESS

    # Mark the socket closed. 
    # The underlying system resource (e.g. a file descriptor)
    # is also closed when all file objects from makefile() are closed.
    # Once that happens, all future operations on the socket object will fail. 
    # The remote end will receive no more data (after queued data is flushed).
    sock.close()

    # return True if port is open or False if port is closed.
    return connected


con = check_port('www.google.com', 83)
print(con)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM