added simpe client
authorMalte Bublitz <me@malte-bublitz.de>
Mon, 21 Jan 2013 17:24:05 +0000 (18:24 +0100)
committerMalte Bublitz <me@malte-bublitz.de>
Mon, 21 Jan 2013 17:24:05 +0000 (18:24 +0100)
client.py [new file with mode: 0644]

diff --git a/client.py b/client.py
new file mode 100644 (file)
index 0000000..03bbb0d
--- /dev/null
+++ b/client.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2013 Malte Bublitz.
+#
+
+import socket
+
+class ChatClient(object):
+       queue = []
+       def __init__(self, ip, port):
+               self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+               self.socket.connect( (ip, port) )
+
+       def send(self, msg):
+               self.socket.send(msg + '\r\n')
+       
+       def name(self, nick):
+               self.send("NAME "+nick)
+
+       def enter(self):
+               self.send("ENTER")
+
+       def one(self, to, msg):
+               self.send("ONE "+to+" "+msg)
+
+       def all(self, msg):
+               self.send("ALL "+msg)
+
+       def exit(self):
+               self.send("EXIT")
+       
+       def recv( self, size = 2048):
+               commands = self.socket.recv(size).split('\r\n')
+               self.queue.extend(commands)
+               
+       def retrieve(self):
+               if len(self.queue):
+                       command = self.queue[0]
+                       self.queue.pop(0)
+                       return command
+               else:
+                       return False
+               
+       
+import thread
+import time
+import select, sys
+
+def autoRecv():
+       while True:
+               chat.recv()
+
+chat = ChatClient('127.0.0.1', 7070)
+chat.name("Client")
+chat.enter()
+chat.all("Hi!")
+chat.one("malte70", "Hello, Malte.")
+while True:
+       x = chat.retrieve()
+       if x != False:
+               print x
+       if select.select([sys.stdin,],[],[],0.0)[0]:
+               chat.send(sys.stdin.readline())
+
+chat.exit()