使用Python编写简单的端口扫描器的实例分享
作者:蚍蜉撼草木 时间:2023-07-20 20:18:32
单线程实现
单线程实现道理比较简单,这里尝试Soket连接3389,连接成功说明端口开放,否则说明没有开远程服务。随便修改了一下就ok了,代码如下,最终得到自己的IP地址。
#!/usr/bin/env python
import socket
if __name__=='__main__':
port=3389
s=socket.socket()
for cnt in range(253,2,-1):
address='XXX.XXX.XXX.'+str(cnt) #XXX.XXX.XXX IP网段
try:
s.connect((address,port))
print address
except socket.error,e:
print 'Error OR Port Not Opened'
Python的代码简单明了,但是功能不简单,速度有些慢,主要还是单线程和网络的原因吧。改进一下可以当一个简陋的端口扫描器使用了,扫描指定网段、指定端口,多线程速度可能能好一点吧。
多线程实现
前几天看了个讲使用Python扫描端口的教程,看了之后自己也写了个扫描端口的脚本。记录下来,方便自己以后回顾。
端口扫描端口效果图
python扫描器源代码
# -*- coding:utf8 -*-
#!/usr/bin/python
# Python: 2.7.8
# Platform: Windows
# Authro: wucl
# Program: 端口扫描
# History: 2015.6.1
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()
s.close()
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:\n')
lock=thread.allocate_lock()
ip_scan(url)
标签:Python,端口扫描
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一道求$b相对于$a的相对路径的php代码
2023-07-12 05:37:46
WEB前端开发高性能优化之JavaScript优化细节
2009-06-10 14:38:00
Python3远程监控程序的实现方法
2021-07-15 15:03:50
关于python DataFrame的合并方法总结
2022-02-02 14:13:58
解决matplotlib库show()方法不显示图片的问题
2021-08-25 19:53:34
Python从ZabbixAPI获取信息及实现Zabbix-API 监控的方法
2021-01-16 18:41:38
![](https://img.aspxhome.com/file/2023/1/90711_0s.png)
推荐四款可视化工具(解决99%的可视化大屏需求)
2022-05-06 03:37:39
![](https://img.aspxhome.com/file/2023/4/127204_0s.jpg)
python搭建服务器实现两个Android客户端间收发消息
2022-05-24 12:21:12
ASP常见数学函数 Abs Atn Cos 等详解
2008-05-28 12:33:00
用python建立两个Y轴的XY曲线图方法
2023-06-30 15:01:26
![](https://img.aspxhome.com/file/2023/0/100850_0s.jpg)
Linux中Python 环境软件包安装步骤
2021-04-10 02:41:38
python3实现磁盘空间监控
2023-07-09 14:12:49
python解压TAR文件至指定文件夹的实例
2023-09-29 04:00:18
python2.7 json 转换日期的处理的示例
2021-02-10 12:39:50
基于Mediapipe+Opencv实现手势检测功能
2023-09-18 13:11:52
![](https://img.aspxhome.com/file/2023/8/114748_0s.png)
Vue 2.0+Vue-router构建一个简单的单页应用(附源码)
2024-05-10 14:17:43
![](https://img.aspxhome.com/file/2023/0/126460_0s.png)
SQL 中 CASE 表达式的使用方式
2024-01-23 14:18:47
![](https://img.aspxhome.com/file/2023/2/112852_0s.png)
Python实现霍夫圆和椭圆变换代码详解
2022-12-22 19:32:29
![](https://img.aspxhome.com/file/2023/5/120925_0s.png)
Python字典的核心底层原理讲解
2022-03-26 08:31:09
![](https://img.aspxhome.com/file/2023/7/117957_0s.png)
使用Python编写简单的画图板程序的示例教程
2022-04-28 03:22:15
![](https://img.aspxhome.com/file/2023/7/131047_0s.jpg)