Commits

Matt Bone  committed b2f3330

context manager for client (still builds and destroys connection on each send)

  • Participants
  • Parent commits 7082bc9

Comments (0)

Files changed (2)

File memo_client.py

 import socket
+import pickle
+
+class MemoServer(object):
+    
+    def __init__(self, host, port):
+        self.host = host
+        self.port = port
+        self.reconnect()
+
+
+    def reconnect(self):
+        #create an INET, STREAMing socket
+        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.s.connect((self.host, self.port))
+
+        
+    def __enter__(self):
+        return self
+
+
+    def put(self, key, value):
+        msg_parts = (key, value)
+        #TODO just stream to the socket? does that work with pickle?
+        msg_parts_pickled = pickle.dumps(msg_parts)
+        self.s.send(msg_parts_pickled)
+        
+        self.s.close()
+        self.reconnect()
+
+
+    def get(key):
+        pass
+
+
+    def __exit__( self, type, value, tb ):
+        self.s.close()
+
 
 if __name__ == '__main__':
-    #create an INET, STREAMing socket
-    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    #now connect to the web server on port 80
-    # - the normal http port
-    s.connect(("mbone-asus", 8889))
-    s.send(b"asdf:jkl")
-    s.close()
 
-    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    s.connect(("mbone-asus", 8889))
-    s.send(b"qwer:123")
-    s.close()
+    
+    with MemoServer("mbone-desktop", 8889) as memo_server:
+        memo_server.put("asdf", "jkl")
+        memo_server.put("qwer", "123")
+
+
+
+
+

File memo_server.py

 import socket
 import threading
+import pickle
 
 MSG_LEN = 8 #you can have a message of any length as long as it's 8 ;)
 
     def __init__(self, clientsocket):
         self.clientsocket = clientsocket
 
-    # def run(self):
-    #     print("doing something")
-    #     msg = ''
-    #     while len(msg) < MSG_LEN:
-    #         chunk = self.clientsocket.recv(MSG_LEN-len(msg))
-    #         chunk = chunk.decode()
-    #         if chunk == '':
-    #             raise RuntimeError("socket connection broken")
-    #         msg = msg + chunk
-    #     msg_parts = msg.split(":")        
-    #     self.update_memo_queue(msg_parts[0], msg_parts[1])
 
     def run(self):
         print("run()")
-        chunks = []
+        chunks = [] #TODO use a string buffer instead?
         chunk = -1
-        while chunk != "":
+        while chunk != b"":
+            #I don't think this is totally right because I think recv can
+            #get a zero len answer even when the remote side has _not_
+            #closed the connection
             chunk = self.clientsocket.recv(RECV_SIZE)
-            msg += chunk.decode()
-            print(chunk)
-        msg_parts = msg.split(":")        
+            chunks.append(chunk)
+        try:
+            msg_parts = pickle.loads(b"".join(chunks))
+        except EOFError:
+            return
         self.update_memo_queue(msg_parts[0], msg_parts[1])
 
-            
-        
-            
 
     def update_memo_queue(self, key, value):
         #very pessimistic piece of crap to begin
             MEMO_QUEUE[key] = []
         MEMO_QUEUE[key].append(value)
         self.lock.release()
+        
 
 def serve(port=8888):
     #from http://docs.python.org/dev/howto/sockets.html
     serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     #bind the socket to a public host,
     # and a well-known port
-    print("binding to %s" %socket.gethostname())
+    print("binding to %s" % socket.gethostname())
     serversocket.bind((socket.gethostname(), port))
     #become a server socket
     serversocket.listen(5)