Skip to content

Instantly share code, notes, and snippets.

@Matts966
Created August 12, 2019 05:25
Show Gist options
  • Save Matts966/26c9f717c12f2d85a74e63f478371180 to your computer and use it in GitHub Desktop.
Save Matts966/26c9f717c12f2d85a74e63f478371180 to your computer and use it in GitHub Desktop.
Listen broadcast udp packet.
from socket import *
import sys
if len(sys.argv) != 2:
print("Please give me port number.")
else:
try:
port=int(sys.argv[1])
except ValueError:
print('Invalid port number.')
sys.exit(1)
try:
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', port))
while True:
data, address = s.recvfrom(1024)
print((data.decode('utf-8').rstrip()))
except Exception as e:
s.close()
print(e.args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment