1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python > socket > recvfrom() > サンプルプログラム

Last updated at Posted at 2015-11-11
動作確認
CentOS 6.5

pythonでのノンブロッキングUDP受信をしたい。

とりあえず以下を実行

151111a.py
import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
 
while True:
  data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
  print "received message:", data

1つのターミナルにて以下を実行しておく。

$ python 151111a.py

同じPCの別ターミナルにて以下を実行する。

$ echo "test\n" | nc -u 127.0.0.1 5005

そうすると151111a.pyを実行した方が以下のようになる。

$ python 151111a.py 

received message: test\n

送信文字列はtest\nとしているが、testでも受信できた。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?