Thrift Python server code generation

Summary: Yep, it's up and running. We now have full client/server support in all of C++ Java PHP and Python. Well, not quite... there's no PHP server, but honestly who wants one? Actually, if we do want one the framework will support writing is as a PHP file that can be served in apache like a web service (i.e. restserver.php would be thriftserver.php). But now that's rambling and nothing to do with this commit.

Notes: cheever, let's chat about porting your multithreaded Pillar Python server over to Thrift


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664783 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/py/src/transport/TSocket.py b/lib/py/src/transport/TSocket.py
index 52c6118..4ef35d9 100644
--- a/lib/py/src/transport/TSocket.py
+++ b/lib/py/src/transport/TSocket.py
@@ -5,15 +5,14 @@
 
   """Socket implementation of TTransport base."""
 
-  handle = None
-  host = "localhost"
-  port = 9090
-
-  def __init__(self, host, port):
+  def __init__(self, host='localhost', port=9090):
     self.host = host
     self.port = port
     self.handle = None
 
+  def set_handle(self, h):
+    self.handle = h
+
   def isOpen(self):
     return handle != None
 
@@ -36,10 +35,42 @@
 
   def read(self, sz):
     buff = self.handle.recv(sz)
+    if len(buff) == 0:
+      raise Exception('TScket read 0 bytes')
     return buff
 
   def write(self, buff):
-    self.handle.sendall(buff)
+    sent = 0
+    have = len(buff)
+    while sent < have:
+      plus = self.handle.send(buff)
+      if plus == 0:
+        raise Exception('sent 0 bytes')
+      sent += plus
+      buff = buff[plus:]
 
   def flush(self):
     pass
+
+class TServerSocket(TServerTransportBase):
+
+  """Socket implementation of TServerTransport base."""
+
+  def __init__(self, port):
+    self.port = port
+    self.handle = None
+ 
+  def listen(self):
+    self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    self.handle.bind(('', self.port))
+    self.handle.listen(128)
+
+  def accept(self):
+    (client, addr) = self.handle.accept()
+    result = TSocket()
+    result.set_handle(client)
+    return result
+
+  def close(self):
+    self.handle.close()
+    self.handle = None