#coding:utf8
import socket,time,re,thread,os
timeout=3
socket.setdefaulttimeout(timeout)


def socket_port(ip,port):
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    result=s.connect_ex((ip,port))
    if result==0:
        lock.acquire()
        print '%s'%ip+',%s端口通过'%port+'\n'
        lock.release()
        s.close()
    else:
        print '%s'%ip+',%s端口异常'%port+'\n'


def ip_scan(ip):
    print"""
*****************************************           
   端口号为 21,22,23,3389,1521,3940 
***************************************** 
"""
    try:
        print '开始扫描 %s'%ip
        list1=[21,22,23,3389,1521,3940,]
        start_time=time.time()
        for i  in list1:
            thread.start_new_thread(socket_port,(ip,int(i)))
        print "扫描端口完成,总共用时:%.2f"%(time.time()-start_time)+'\n'
    except:
        print "扫描IP出错"


if __name__=='__main__':
    url=open('C:\Users\Jia\Desktop\IP,Port.txt','r')
    url1=url.read()
    url2=re.findall(r'[\d]+.+[^\W]',url1)
    url.close()
    for x in url2:
        lock=thread.allocate_lock()
        ip_scan(x)
        time.sleep(5)