python毎日練習の20170611

1063 ワード

#!/usr/local/bin/python
#-*- coding: UTF-8 -*-

#     
import socket
import thread
import time
socket.setdefaulttimeout(3)  #           

def socket_port(ip,PORT):  #      
    try:
        if PORT>=65535:
            print u"      0-65535"
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #s.settimeout(float(1))  #  5S
        #s.connect((ip,PORT))
        result=s.connect_ex((ip,PORT))
        if(result==0):
            print ip,u":",PORT,u"    "
        s.close()
    except:
        print u"      2"


def IP__port(data): #    
    try:
        #data="127.0.0.1"
        t=time.time()
        for i in range(0,1000 + 1):   #65535
            thread.start_new_thread(socket_port,(data,int(i)))
            time.sleep(0.003) #     Seeker    
        print u'         time:%f' % (time.time()-t)
    except:
        print u"      1"


if __name__=='__main__':
    #socket_port("127.0.0.1",21)  #      
    IP__port("127.0.0.1") #