Skip to content

Instantly share code, notes, and snippets.

@olavmrk
Created January 29, 2014 09:15
Show Gist options
  • Save olavmrk/8684342 to your computer and use it in GitHub Desktop.
Save olavmrk/8684342 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import select
import socket
import struct
import sys
TIMEOUT = 10.0
ip = sys.argv[1]
port = int(sys.argv[2])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', port))
mreq = struct.pack('4sI', socket.inet_aton(ip), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
ready = select.select([sock], [], [], TIMEOUT)
if ready[0]:
exit(0)
else:
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment