Python Scan Port Script

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.