python檢測(cè)遠(yuǎn)程udp端口是否打開(kāi)的方法
本文實(shí)例講述了python檢測(cè)遠(yuǎn)程udp端口是否打開(kāi)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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]))
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
- python UDP(udp)協(xié)議發(fā)送和接收的實(shí)例
- python實(shí)現(xiàn)udp數(shù)據(jù)報(bào)傳輸?shù)姆椒?/a>
- python網(wǎng)絡(luò)編程之UDP通信實(shí)例(含服務(wù)器端、客戶(hù)端、UDP廣播例子)
- python基礎(chǔ)教程之udp端口掃描
- Python的Socket編程過(guò)程中實(shí)現(xiàn)UDP端口復(fù)用的實(shí)例分享
- Python+Socket實(shí)現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能示例
- python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)例分析
- Python基于socket模塊實(shí)現(xiàn)UDP通信功能示例
- Python實(shí)現(xiàn)基于TCP UDP協(xié)議的IPv4 IPv6模式客戶(hù)端和服務(wù)端功能示例
- Python里disconnect UDP套接字的方法
- Python udp網(wǎng)絡(luò)程序?qū)崿F(xiàn)發(fā)送、接收數(shù)據(jù)功能示例
相關(guān)文章
pytorch人工智能之torch.gather算子用法示例
這篇文章主要介紹了pytorch人工智能之torch.gather算子用法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09matlab中imadjust函數(shù)的作用及應(yīng)用舉例
這篇文章主要介紹了matlab中imadjust函數(shù)的作用及應(yīng)用舉例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Python面向?qū)ο笾?lèi)的內(nèi)置attr屬性示例
這篇文章主要介紹了Python面向?qū)ο笾?lèi)的內(nèi)置attr屬性,結(jié)合實(shí)例形式分析了Python面向?qū)ο笾蓄?lèi)的屬性相關(guān)定義、賦值、修改等操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12python逐行讀寫(xiě)txt文件的實(shí)例講解
下面小編就為大家分享一篇python逐行讀寫(xiě)txt文件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04python讀取測(cè)試數(shù)據(jù)的多種方式
本文主要介紹了python讀取測(cè)試數(shù)據(jù)的多種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08python中@property的作用和getter setter的解釋
這篇文章主要介紹了python中@property的作用和getter setter的解釋,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12如何用Python實(shí)現(xiàn)自動(dòng)發(fā)送微博
大家好,本篇文章主要講的是如何用Python實(shí)現(xiàn)自動(dòng)發(fā)送微博,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏2022-01-01Python 列表(List) 的三種遍歷方法實(shí)例 詳解
這篇文章主要介紹了Python 列表(List) 的三種遍歷方法實(shí)例 詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04