• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python检测远程udp端口是否打开的方法

python检测远程udp端口是否打开的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python udp,python socket udp,python udp编程,python3 udp,python udp server等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python检测远程udp端口是否打开的方法。分享给大家供大家参考。具体实现方法如下:

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]))</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python网络编程之数据传输UDP实例分析
  • python检测远程udp端口是否打开的方法
  • python检测远程udp端口是否打开的方法
  • python基础教程之udp端口扫描

相关文章

  • 一个计算身份证号码校验位的Python小程序
  • 在Python程序中操作文件之isatty()方法的使用教程
  • urllib2自定义opener详解
  • python杀死一个线程的方法
  • pytyon 带有重复的全排列
  • Python的ORM框架SQLObject入门实例
  • 使用基于Python的Tornado框架的HTTP客户端的教程
  • Python使用PIL库实现验证码图片的方法
  • python编码最佳实践之总结
  • Python群发邮件实例代码

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 详解在Python程序中使用Cookie的教程
    • Python使用Paramiko模块编写脚本进行远程服务器操作
    • 利用打码兔和超人打码自封装的打码类分享
    • Python简单检测文本类型的2种方法【基于文件头及cchardet库】
    • python实现解数独程序代码
    • 深入解析Python中函数的参数与作用域
    • python抓取网页内容示例分享
    • 使用Python写CUDA程序的方法
    • 使用Python进行二进制文件读写的简单方法(推荐)
    • Python爬虫利用cookie实现模拟登陆实例详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有