python检测远程udp端口是否打开的方法
更新时间:2015年03月14日 15:16:15 作者:chongq
这篇文章主要介绍了python检测远程udp端口是否打开的方法,涉及Python操作socket实现检测udp端口的技巧,需要的朋友可以参考下
本文实例讲述了python检测远程udp端口是否打开的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
import socket
import threading
import time
import struct
import Queue
queue = Queue.Queue()
def udp_sender(ip,port):
try:
ADDR = (ip,port)
sock_udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock_udp.sendto("abcd...",ADDR)
sock_udp.close()
except:
pass
def icmp_receiver(ip,port):
icmp = socket.getprotobyname("icmp")
try:
sock_icmp = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
except socket.error, (errno, msg):
if errno == 1:
# Operation not permitted
msg = msg + (
" - Note that ICMP messages can only be sent from processes"
" running as root."
)
raise socket.error(msg)
raise # raise the original error
sock_icmp.settimeout(3)
try:
recPacket,addr = sock_icmp.recvfrom(64)
except:
queue.put(True)
return
icmpHeader = recPacket[20:28]
icmpPort = int(recPacket.encode('hex')[100:104],16)
head_type, code, checksum, packetID, sequence = struct.unpack(
"bbHHh", icmpHeader
)
sock_icmp.close()
if code == 3 and icmpPort == port and addr[0] == ip:
queue.put(False)
return
def checker_udp(ip,port):
thread_udp = threading.Thread(target=udp_sender,args=(ip,port))
thread_icmp = threading.Thread(target=icmp_receiver,args=(ip,port))
thread_udp.daemon= True
thread_icmp.daemon = True
thread_icmp.start()
time.sleep(0.1)
thread_udp.start()
import threading
import time
import struct
import Queue
queue = Queue.Queue()
def udp_sender(ip,port):
try:
ADDR = (ip,port)
sock_udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock_udp.sendto("abcd...",ADDR)
sock_udp.close()
except:
pass
def icmp_receiver(ip,port):
icmp = socket.getprotobyname("icmp")
try:
sock_icmp = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
except socket.error, (errno, msg):
if errno == 1:
# Operation not permitted
msg = msg + (
" - Note that ICMP messages can only be sent from processes"
" running as root."
)
raise socket.error(msg)
raise # raise the original error
sock_icmp.settimeout(3)
try:
recPacket,addr = sock_icmp.recvfrom(64)
except:
queue.put(True)
return
icmpHeader = recPacket[20:28]
icmpPort = int(recPacket.encode('hex')[100:104],16)
head_type, code, checksum, packetID, sequence = struct.unpack(
"bbHHh", icmpHeader
)
sock_icmp.close()
if code == 3 and icmpPort == port and addr[0] == ip:
queue.put(False)
return
def checker_udp(ip,port):
thread_udp = threading.Thread(target=udp_sender,args=(ip,port))
thread_icmp = threading.Thread(target=icmp_receiver,args=(ip,port))
thread_udp.daemon= True
thread_icmp.daemon = True
thread_icmp.start()
time.sleep(0.1)
thread_udp.start()
thread_icmp.join()
thread_udp.join()
return queue.get(False)
if __name__ == '__main__':
import sys
print checker_udp(sys.argv[1],int(sys.argv[2]))
希望本文所述对大家的Python程序设计有所帮助。
您可能感兴趣的文章:
- Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】
- python UDP(udp)协议发送和接收的实例
- python实现udp数据报传输的方法
- python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
- python基础教程之udp端口扫描
- Python的Socket编程过程中实现UDP端口复用的实例分享
- Python+Socket实现基于UDP协议的局域网广播功能示例
- python网络编程之数据传输UDP实例分析
- Python基于socket模块实现UDP通信功能示例
- Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能示例
- Python里disconnect UDP套接字的方法
- Python udp网络程序实现发送、接收数据功能示例
相关文章
详解如何在python中读写和存储matlab的数据文件(*.mat)
这篇文章主要介绍了详解如何在python中读写和存储matlab的数据文件(*.mat),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-02-02Python 安装setuptools和pip工具操作方法(必看)
下面小编就为大家带来一篇Python 安装setuptools和pip工具操作方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-05-05python基于json文件实现的gearman任务自动重启代码实例
这篇文章主要介绍了python基于json文件实现的gearman任务自动重启代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-08-08
最新评论