一个python端口占用监测的程序,该程序可以监测指定IP的端口是否被占用。

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import socket, time, thread
socket.setdefaulttimeout(3) #设置默认超时时间

def socket_port(ip, port):
    """
    输入IP和端口号,扫描判断端口是否占用
    """
    try:
        if port >=65535:
            print u'端口扫描结束'
        s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result=s.connect_ex((ip, port))
        if result==0:
            lock.acquire()
            print ip,u':',port,u'端口已占用'
            lock.release()
    except:
        print u'端口扫描异常'

def ip_scan(ip):
    """
    输入IP,扫描IP的0-65534端口情况
    """
    try:
        print u'开始扫描 %s' % ip
        start_time=time.time()
        for i in range(0,65534):
            thread.start_new_thread(socket_port,(ip, int(i)))
        print u'扫描端口完成,总共用时:%.2f' %(time.time()-start_time)
#       raw_input("Press Enter to Exit")
    except:
        print u'扫描ip出错'

if __name__=='__main__':
    url=raw_input('Input the ip you want to scan: ')
    lock=thread.allocate_lock()
    ip_scan(url)

该程序执行结果如下:

# python scan_port.py
Input the ip you want to scan: 20.0.208.112
开始扫描 20.0.208.112
20.0.208.112 : 111 端口已占用
20.0.208.112 : 22 端口已占用
20.0.208.112 : 8000 端口已占用
20.0.208.112 : 15996 端口已占用
20.0.208.112 : 41734 端口已占用
扫描端口完成,总共用时:9.38

最新文章

  1. 【初级】linux mv 命令详解及使用方法实战
  2. dataview将excel表格的数据导出成txt文件
  3. 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq
  4. Azure 数据库中文乱码的问题
  5. BZOJ3160: 万径人踪灭
  6. 【LeetCode】Power of Two
  7. Spring MVC 3.0 请求转发和重定向
  8. js去除重复数值
  9. 【笔试】T实习生2014 总结
  10. [Android] PorterDuff使用实例----实现新浪微博图片下载效果
  11. javascript:DOM自定义属性的妙用
  12. 聊聊React高阶组件(Higher-Order Components)
  13. LeetCode & Q219-Contains Duplicate II
  14. 后端开发者的Vue学习之路(五)
  15. springBoot总结
  16. 【XSY2785】模型
  17. shell script中read的用法
  18. 初识zookeeper(1)之zookeeper的安装及配置
  19. 网页使用思源字体 CSS
  20. python 基础5 初级函数

热门文章

  1. 海外Essay写作如何减少重复用词
  2. IDEA--安装
  3. Ubuntu13.04闪屏的问题
  4. cors跨域和jsonp劫持漏洞 和 同源策略和跨域请求解决方案
  5. C# 串口编程,扫码枪使用
  6. dac FDMemTable
  7. UVALive 3942 字典树+dp
  8. [Security] Web Security Essentials
  9. Linux--CentOS7启用网卡
  10. Springboot项目绑定域名,使用Nginx配置Https