Skip to content

Instantly share code, notes, and snippets.

@y-ookuma
Last active November 11, 2021 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-ookuma/7423505ad66a8342c3825585849774ae to your computer and use it in GitHub Desktop.
Save y-ookuma/7423505ad66a8342c3825585849774ae to your computer and use it in GitHub Desktop.
from socket import *
import time as t
## UDP受信クラス
class udprecv():
def __init__(self):
SrcIP = "" # 受信元IP
SrcPort = 16520 # 受信元ポート番号
self.SrcAddr = (SrcIP, SrcPort) # アドレスをtupleに格納
self.BUFSIZE = 512 # バッファサイズ指定
self.udpServSock = socket(AF_INET, SOCK_DGRAM) # ソケット作成
self.udpServSock.bind(self.SrcAddr) # 受信元アドレスでバインド
def recv(self,debug=False,debug_sec=10):
if debug:
start=t.time()
while True: # 常に受信待ち
data, addr = self.udpServSock.recvfrom(self.BUFSIZE)
# 受信
print(data.decode(), addr) # 受信データと送信アドレス表示
if debug:
end=t.time()
if end-start>=debug_sec:
print("debug_time:",end-start)
break;
#受信
udp = udprecv() # クラス呼び出し
udp.recv(debug=True) # 関数実行
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment