Last active
April 20, 2018 04:32
-
-
Save glegoux/ddbddc5f2c741c9f5d899ea14941becc to your computer and use it in GitHub Desktop.
[Pyhton] Run UDP server via socket server
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # | |
| # udpserver.py | |
| # | |
| # See Gist udpclient.py to send a UDP message. | |
| # | |
| # Run UDP server on 127.0.0.1 port 8000. | |
| # | |
| # usage: python3 udpserver.py | |
| if __name__ == "__main__": | |
| import socket | |
| UDP_IP_ADDRESS = "127.0.0.1" | |
| UDP_PORT = 8000 | |
| # listen for UDP messages | |
| server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
| server.bind((UDP_IP_ADDRESS, UDP_PORT)) | |
| print("Serving UDP on {} port {} ...".format(UDP_IP_ADDRESS, UDP_PORT)) | |
| while True: | |
| data, addr = server.recvfrom(1024) | |
| print("Received message: '{}'".format(data.decode('utf-8'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment