This article mainly introduces how to check whether a specified port is opened by scanning the IP segment of Python, and related skills related to Port scanning by using the socket module of Python, for more information about how to check whether a specified port is opened, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
#! /Usr/local/bin/python #-*-coding: UTF-8 -*-#################################### ######################################## ######################################## ### BLOG: http://hi.baidu.com/alalmn# Whether the specified port of the Python IP address segment is open ################################ ################# import socketimport threading, timesocket. setdefatimetimeout (10) # set the global default timeout # check whether class socket_port (threading) is enabled on the IP port. thread): def _ init _ (self, cond, name): super (socket_port, self ). _ init _ () self. cond = cond self. cond. set () # set the identification space to Ture self. HOST = name def run (self): # time. sleep (1) # Make sure to first run the method try: PORT = 21 s = socket in Seeker. socket (s Ocket. AF_INET, socket. SOCK_STREAM) s. connect (self. HOST, PORT) print "" print self. HOST, u ":", PORT, u "PORT open" # self. cond. wait () # blocks the thread until the internal identifier of the Event object is set to True or times out (if the timeout parameter is provided ). Self. cond. set () # set the flag bit to true return 1 failed t: print ". ", # print self. HOST, u ":", PORT, u "PORT is not open" # self. cond. wait () # blocks the thread until the internal identifier of the Event object is set to True or times out (if the timeout parameter is provided ). Self. cond. set () # set the identification space to Ture return 0 ### socket_port ("192.168.2.1") # if socket_port ("192.168.2.100"): # print "open" # else: # print "not open" def ip2num (ip): ip = [int (x) for x in ip. split ('. ')] return ip [0] <24 | ip [1] <16 | ip [2] <8 | ip [3] def num2ip (num ): # time. sleep (0.05) #50 ms # time. sleep (0.1) # s # data = '% s. % s. % s. % s' % (num & 0xff000000)> 24, # (num & 0x00ff0000)> 16, # (num & 0x0000ff00)> 8, # num & 0x000000ff) # socket_port (data) # check whether the IP port is open if num> = IPend: print u "IP import array complete" return '% s. % s. % s. % s' % (num & 0xff000000)> 24, (num & 0x00ff0000)> 16, (num & 0x0000ff00)> 8, num & 0x000000ff) def gen_ip (ip1, ip2): # returns an array # ip # global IPend # start, IPend = [ip2num (x) for x in ip. split ('-')] global IPend = ip2 return [num2ip (num) for num in range (ip1, ip2 + 1) if num & 0xff] import iniif _ name __= = '_ main _': ini. ini_get () # read INI list_ip = gen_ip (ip2num (ini. IP1), ip2num (ini. IP2) I1 = 0 # get the first element of list print u "start scanning IP" ip = 0 while I1 <len (list_ip): # print list_ip [I1] time. sleep (0.3) # ensure that the method cond = threading in Seeker is run first. event () hider = socket_port (cond, list_ip [I1]) hider. start () if ip >=255: ini. ini_write (list_ip [I1], ini. IP2) # modify INI print ip address ip = 0 ip = ip + 1 I1 = I1 + 1 # layer
Ini. py:
#! /Usr/local/bin/python #-*-coding: UTF-8 -*-#################################### ############## qq: 316118740 # BLOG: http://hi.baidu.com/alalmn# How to operate the INI file in Python # sorry for the poor data I just learned ############################# #################### IP1 = "" # scan IPIP2 = "" # IPINITXT =" IP. ini "# INI file name import ConfigParserdef ini_get (): # read ini try: global IP1 global IP2 global INITXT config = ConfigParser. configParser () config. readfp (open (INITXT) IP1 = config. get ("ipdata", "ip1") IP2 = config. get ("ipdata", "ip2") failed t: print "read INI error" ini_add ("", "") # write INI def ini_add (ip1, ip2 ): # write INI try: global INITXT config = ConfigParser. configParser () config. add_section ("ipdata") # set the section and the corresponding value config. set ("ipdata", "ip1", ip1) config. set ("ipdata", "ip2", ip2) config. write (open (INITXT, "w") # write the file into T: print "write INI error" def ini_write (ip1, ip2): # modify INI try: global INITXT config = ConfigParser. configParser () config. read (INITXT) if not config. has_section ("ipdata"): # check whether this Section exists. if it does not exist, create temp = config. add_section ("") config. set ("ipdata", "ip1", ip1) config. set ("ipdata", "ip2", ip2) config. write (open (INITXT, "r +") failed t: print "INI error modified" ini_add ("","") # write INI # if _ name __= = '_ main _': # ini_get () # read INI ## print IP1 ## print IP2 ### ini_add ("222222222", "3333333333333") # write INI ## ini_get () # read INI ## print IP1 ## print IP2 ## ini_write ("999999999", "0000000000") # modify INI # ini_get () # read INI # print IP1 # print IP2
Run:
The code is as follows:
Python TCP21.py
I hope this article will help you with Python programming.