Python scans IP segment to see if the specified port is open

Source: Internet
Author: User
Tags config ini sleep socket split

The example in this article describes how the Python scan IP segment will see whether the specified port is open. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80-81 #!/usr/local/bin/python #-*-coding:utf-8-*-#################################################################### ################################################## #BLOG: HTTP://HI.BAIDU.COM/ALALMN # Python Scan IP segment Specifies whether the port is open ######## ########################################## Import Socket Import threading,time socket.setdefaulttimeout (10) # Set the global default timeout #查看IP端口是否开放 class Socket_port (threading. Thread): Def __init__ (Self,cond, name): Super (Socket_port, self). __init__ () Self.cond = Cond Self.cond.set () #将标识位设为Ture Self. HOST = name def run (self): #time. Sleep (1) #确保先运行Seeker中的方法 try:port=21 s = socket.socket (socket.af_inet, socket. SOCK_STREAM) S.connect (self. Host,port)) print "" Print self. Host,u ":", Port,u "Port open" #self. Cond.wait () #堵塞线程 until the event object's internal identity bit is set to TRUE or timeout (if a parameter timeout is provided). Self.cond.set () #将标识位设为Ture return 1 Except:print ".", #print self. Host,u ":", port,u "Port not Open" #self. Cond.wait () #堵塞线程 until the internal identity bit of the event object is set to TRUE or timeout (if a parameter timeout is provided). Self.cond.set () #将标识位设为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) #50ms #time. Sleep (0.1) #s # data= '%s.%s.%s.%s ' (num & 0xff000000) >&gt ; # (Num & 0x00ff0000) >>, # (num & 0x0000ff00) >> 8, # num & 0x000000ff) # #socket_port (dat A) #查看IP端口是否开放 if num>=ipend:print u "IP import array Complete" return '%s.%s.%s.%s '% ((num & 0xff000000) >>, (Num & 0x00ff0000) >> (num & 0x0000ff00) >> 8, Num & 0X000000FF) def gen_ip (IP1,IP2): #返回数组 # ip # glob Al Ipend # start, ipend = [Ip2num (x) for x in Ip.split ('-')] global ipend ipend=ip2 return [Num2ip (num) for NUM in range (i p1,ip2+1) if num & 0xff] Import ini if __name__== ' __main__ ': Ini.ini_get () #读取INI list_ip=gen_ip (INI. IP1), ip2num (INI). IP2) I1 = 0 #得到list的第一个元素 print u "start scanning IP" ip=0 while I1 < Len (LIST_IP):#print List_ip[i1] Time.sleep (0.3) #确保先运行Seeker中的方法 cond = Threading. Event () Hider = Socket_port (Cond,list_ip[i1]) Hider.start () if Ip>=255:ini.ini_write (List_ip[i1],ini. IP2) #修改INI Print IP ip=0 ip=ip+1 I1 = I1 + 1 #一层

ini.py:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59-60 #!/usr/local/bin/python #-*-, Coding:utf-8-*-, ############################### ################### #qq: 316118740 #BLOG: HTTP://HI.BAIDU.COM/ALALMN # Python Operation INI File # Just learn to write well please forgive me #################### ############################## ip1= "" #扫描IP ip2= "" #当前已经扫到的IP initxt= "Ip.ini" #INI文件名字 import Configparser def ini_get () : #读取INI try:global IP1 Global IP2 global initxt config = Configparser.configparser () CONFIG.READFP (open (initxt)) IP1 = C Onfig.get ("Ipdata", "ip1") IP2 = Config.get ("Ipdata", "IP2") except:print "read ini error" Ini_add ("", "") #写入INI   def ini_a DD (IP1,IP2): #写入INI try:global initxt config = configparser.configparser () config.add_section ("Ipdata") # Set up section sections and corresponding values Config.set ("Ipdata", "ip1", ip1) COnfig.set ("Ipdata", "IP2", IP2) config.write (open (Initxt, "w")) # Write to File except: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"): # To see if the section exists and does not exist then create temp = Config.add_section ("") Config.set ("Ipdata", "ip1", ip1) Config.set ("Ipdata", "IP2", IP2) Config.write (Open (Initxt, r+)) except:print "Modify INI Error" Ini_add ("" "") #写入INI #if __name__== ' __main__ ': # # Ini_get () #读取 INI # # Print IP1 # # print IP2 # # Ini_add ("222222222", "3333333333333") #写入INI # # Ini_get () #读取INI # # Print IP1 # # print I P2 # ini_write ("999999999", "0000000000") #修改INI # Ini_get () #读取INI # print IP1 # print IP2

Run:

The code is as follows:

Python tcp21.py

I hope this article will help you with your Python programming.

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.