Python scans IP segments to check whether the specified port is open.

Source: Internet
Author: User

Python scans IP segments to check whether the specified port is open.

This example describes how to check whether a specified port is opened by scanning the IP segment of Python. 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# Python scan IP segment specified port 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 (socket. 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# Python operation INI file # Just learning to write is not good, please forgive me ############################ ##################### 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:
Copy codeThe Code is as follows: python TCP21.py

I hope this article will help you with Python programming.

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.