#!/usr/bin/python
#coding =utf-8
‘‘‘
First, the class method of the socket module:
Socket.socket (Family,type)--Creates and returns a new socket object
SOCKET.GETFQDN (name)-Converts an IP address string with a well-segmented location into a full domain name
Socket.gethostbyname (name)--resolves the hostname to an IP address string separated by a dot number
SOCKET.FROMFD (Fd,family,type)--Create a socket object from an existing file descriptor
Second, the Socket module instance method:
Sock.bind ((Adrs,port))--binds the socket to an address and port
Sock.accept ()--Returns a client socket (with client-side address information)
Sock.listen--Set the socket as a listening mode to monitor the backlog of incoming link requests
Sock.connect ((Adrs,port))--Connect the socket to the defined host and port
SOCK.RECV (Buflen[,flags])--accepts data from a socket up to Buflen characters
Sock.send (Data[,flags])--send data via socket
Sock.close ()--close socket
Sock.getsockopt (lvl,optname)--Gets the value of the specified socket option
Sock.setsockopt (Lvl,optname)--Set the value of the socket option
‘‘‘
Import socket, time, thread
Socket.setdefaulttimeout (3)
def socket_port (Ip,port):
"""
Enter IP and port number, scan to determine if port is open
"""
Try
If port>=65535:
Print U ' Port scan end '
S=socket.socket (socket.af_inet, socket. SOCK_STREAM)
RESULT=S.CONNECT_EX ((Ip,port))
If result==0:
Lock.acquire ()
Print Ip,u ': ', Port,u ' port open '
Lock.release ()
S.close ()
Except
Print U ' Port scan exception '
def ip_scan (IP):
"""
Input IP, scan the IP 0-65534 port case
"""
Try
Print u ' Start scan%s '% IP
Start_time=time.time ()
#for i in range (0,65534):
For I in Range (0,3308):
Thread.start_new_thread (Socket_port, (Ip,int (i)))
Print u ' Scan port complete, total time:%.2f '% (Time.time ()-start_time)
Raw_input ("Press Enter to Exit")
Except
Print u ' scan IP error '
If __name__== ' __main__ ':
Url=raw_input (' Input the IP want to scan:\n ')
Lock=thread.allocate_lock ()
Ip_scan (URL)
Python Scan Port Script