Initial commit
[0share.git] / client.py
blob92938044b08afe186230f15e6fa03adb3f87a30b
1 # Copyright (C) 2008, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 from logging import info, debug
5 import socket, sys, select
7 #broadcast = ('<broadcast>', 308339)
8 broadcast = ('localhost', 308339)
10 def fetch(digests):
11 if not digests:
12 print >>sys.stderr, "No digests given!"
13 sys.exit(1)
15 info("Broadcasting request for %s on local network...", digests)
17 client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
18 client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
20 client.sendto('0share\n' + '\n'.join(digests), broadcast)
22 while True:
23 r = select.select([client], [], [], 1)[0]
24 if r:
25 data, addr = client.recvfrom(128)
26 addr = addr[0]
27 lines = data.split('\n')
28 if lines[0] == '0share-reply':
29 for d in lines[1:]:
30 print "%s has %s" % (addr, d)
31 else:
32 info("Invalid reply from %s: %s", addr, repr(data))
33 else:
34 break
35 info("No further responses for one second")