python查看端口占用及计时模块

来源:互联网 发布:汉字笔顺在线演示软件 编辑:程序博客网 时间:2024/05/06 10:59
import socketimport timeitdef isUse(ip, port):    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    try:        s.connect((ip, int(port)))        s.shutdown(2)#shutdown参数表示后续可否读写        # print '%d is ok' % port        return True    except Exception, e:        print e        return Falsedef findPort():    for i in range(1, 65535):        if IsUse('127.0.0.1', i):            print i#findPort遍历1-65535返回已占用端口。if __name__ == '__main__':    print timeit.timeit("findPort()", 'from __main__ import findPort', number=1)    #timeit模块提供计时功能,number为运行多少次数取平均值
0 0
原创粉丝点击