Server - basic send/recv client - server functionality
authorAdriana Draghici <adriana008@gmail.com>
Tue, 19 Jan 2010 13:17:03 +0000 (15:17 +0200)
committerAdriana Draghici <adriana008@gmail.com>
Tue, 19 Jan 2010 13:17:03 +0000 (15:17 +0200)
server/Client.py [new file with mode: 0644]
server/Server.py

diff --git a/server/Client.py b/server/Client.py
new file mode 100644 (file)
index 0000000..19e497b
--- /dev/null
@@ -0,0 +1,44 @@
+import sys, socket
+MSGLEN = 100
+
+
+class MySocket:
+       def __init__(self, sock=None):
+               if sock is None:
+                       self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+               else:
+                       self.sock = sock
+
+       def connect(self, host, port):
+               self.sock.connect((host, port))
+               print "connected to %s %s"%(host, port)
+
+       def send_msg(self, msg):
+               totalsent = 0
+               while totalsent < len(msg):
+                       sent = self.sock.send(msg[totalsent:])
+                       if sent == 0:
+                               raise RuntimeError,     "socket connection broken"
+                       totalsent = totalsent + sent
+
+       def recv_msg(self):
+               msg = ''
+               chunk = self.sock.recv(MSGLEN)
+               if chunk == '':
+                               raise RuntimeError,     "socket connection broken"
+               msg = msg + chunk
+               print msg
+               return msg
+
+
+if __name__ == "__main__":
+
+       s = MySocket()
+       s.connect("127.0.0.1", 10001)
+       s.send_msg("hello")
+       s.recv_msg()
+       
+
+
+
+
index 6723520..cd28d47 100644 (file)
@@ -2,12 +2,20 @@
 
 # TODO: listen, receive on port 
 
-import sys, time, os
+import sys, os, socket 
 from daemon import Daemon
 
 def doServer():
+       #os.spawnvp(os.P_NOWAIT,"/usr/bin/transmission",[])
+       serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+       serversocket.bind(("127.0.0.1", 10001));
+       serversocket.listen(10) #max 10 requests
+       while(1):
+               (clientsock, address) = serversocket.accept();
+               clientsock.recv(100)
+               clientsock.send("Hello :)")
+       clientsock.close()      
 
-       os.spawnvp(os.P_NOWAIT,"/usr/bin/transmission",[])
 
 class MyDaemon(Daemon):
        def run(self):