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