-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
executable file
·45 lines (29 loc) · 1.07 KB
/
chat.py
File metadata and controls
executable file
·45 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import select
import sys
import networkzero as nw0
try:
input = raw_input
except NameError:
pass
def main():
username = input("Please enter your username: ")
address = nw0.advertise("zerochat_" + username)
oldchats = {}
while True:
chats = {name:address for name, address in nw0.discover_all() if name.startswith("zerochat_")}
if chats.keys() != oldchats.keys():
print("New users:")
print(chats)
rlist, wlist, elist = select.select([sys.stdin], [], [], 0.1)
if rlist:
recipient = sys.stdin.readline().rstrip()
msg = input("Message:").rstrip()
print("\x1b[31;m[%s] %s\x1b[0m" % (recipient, msg))
recipient_service = nw0.discover("zerochat_" + recipient)
nw0.send_message_to(recipient_service, msg)
received_msg = nw0.wait_for_message_from(address, wait_for_s=0, autoreply=True)
if received_msg is not None:
print("Got message: %s" % received_msg)
oldchats = chats
if __name__ == '__main__':
main()