Last active
May 22, 2018 08:07
-
-
Save SystemsAndroidRobotics/31972c39782cdf20a1f3e96dd1dadf80 to your computer and use it in GitHub Desktop.
非常に単純なUDP送信コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"UDP通信でデータを送る最も単純なコード Hisashi Ishihara 2018-04-24" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import socket #通信用\n", | |
"\n", | |
"address = ('127.0.0.1', 1308) #送信相手のIPアドレスと通信に使うポート番号の設定\n", | |
"msg = '110,110,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0' #送信する文字列\n", | |
"\n", | |
"udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #udpという名前のUDPソケットを生成\n", | |
"\n", | |
"while True:#送信ループ\n", | |
" try:\n", | |
" udp.sendto(msg.encode(), address) #文字列をバイトデータに変換してaddress宛に送信\n", | |
" except KeyboardInterrupt:#強制終了を検知したらソケットを閉じる\n", | |
" udp.close()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment