sending more then 2 messages at a time to a SocketServer fails

MRAB python at mrabarnett.plus.com
Tue Nov 1 13:56:25 EDT 2011


On 01/11/2011 15:07, MrSmile wrote:
> Hi people!
> I have asked myself why I am not capable sending 2 messages a time to a
> Socketserver. Why is that?!
>
>
> Here the Server:
>
> import SocketServer
> from ast import literal_eval
>
> class MKTest(object):
>      DSX = []
>      MKTestInst = None
>
>      def __init__(self,Daten):
>          MKTest.DSX.append(Daten)
>
>      def getObj(Daten):
>          if MKTest.MKTestInst == None:
>              MKTest.MKTestInst = MKTest(Daten)
>          return MKTest.MKTestInst
>
>      getObj = staticmethod(getObj)
>
> class MySockX(SocketServer.BaseRequestHandler):
>      def handle(self):
>          data = self.request.recv(1024)
>          data = literal_eval(data)
>          #MKTest.getObj(data[0])
>          #MKObj = MKTest(data[0])
>          MKObj = MKTest.getObj(data[0])
>          data = MKTest.DSX
>          data = '%s' % data
>          self.request.send(data)
>
> if __name__ == "__main__":
>      HOST, PORT = "localhost", 9999
>      server = SocketServer.TCPServer((HOST,PORT),MySockX)
>      server.serve_forever()
>
[snip]

I think that in the 'handle' function you should be putting the code in
a 'while' loop:

      def handle(self):
          # The client has opened a socket to the server.
          while True:
              data = self.request.recv(1024)
              if not data:
                  # The client has closed the socket to the server.
                  break
              ...



More information about the Python-list mailing list