Created
June 24, 2012 18:40
-
-
Save ZhanruiLiang/2984350 to your computer and use it in GitHub Desktop.
Where are you & im here
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
"i'm here" | |
import socket | |
import getpass | |
from time import sleep | |
Port = 8789 | |
User = getpass.getuser() | |
Delta = 3.0 | |
dest = ('<broadcast>' , Port) | |
s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET , socket.SO_BROADCAST, 1) | |
while 1: | |
s.sendto("%s is here" % User, dest) | |
sleep(Delta) |
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
"where are you?" | |
import socket, sys | |
import re | |
Port = 8789 | |
found = [] | |
s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET , socket.SO_BROADCAST , 1) | |
s.setsockopt(socket.SOL_SOCKET , socket.SO_REUSEADDR , 1) | |
s.bind(('', Port)) | |
patt = re.compile(r'(.+) is here') | |
while True: | |
mess, addr = s.recvfrom(1024) | |
try: | |
user = patt.match(mess).groups()[0] | |
if (user, addr) not in found: | |
print 'found %s at %s' % (user, addr) | |
found.append((user, addr)) | |
except AttributeError: | |
print >> sys.stderr, 'message [%s] not recognized' % mess |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment