forked from f-prime/CentralChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
36 lines (33 loc) · 1.09 KB
/
Copy pathclient.py
File metadata and controls
36 lines (33 loc) · 1.09 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
import socket
import json
import threading
import random
class CentralClient:
def __init__(self, room, nick):
self.ip = "centralchat.zapto.org"
self.port = 5124
self.nick = nick
self.sock = socket.socket()
self.room = room
self.id = str(random.random())
def main(self):
self.sock.connect((self.ip, self.port))
threading.Thread(target=self.listen).start()
while True:
msg = raw_input(self.nick+": ")
msg = json.dumps({"id":self.id,"nick":self.nick, "room":self.room, "msg":msg})
self.sock.send(msg)
def listen(self):
while True:
data = self.sock.recv(1024)
if data:
try:
data = json.loads(data)
except:
continue
if data['room'] == self.room and data['id'] != self.id and data['msg'] != '':
print data['nick']+": "+data['msg']
if __name__ == "__main__":
nick = raw_input("Nick: ")
room = raw_input("Room: ")
CentralClient(room, nick).main()