async icon indicating copy to clipboard operation
async copied to clipboard

Can't read from python TCP server

Open eulerreich opened this issue 9 years ago • 1 comments

I have a python TCP server adapted from the example in https://docs.python.org/2/library/socketserver.html.

class CommandTCPHandler(SocketServer.StreamRequestHandler):

    def handle(self):
        # self.rfile is a file-like object created by the handler;
        # we can now use e.g. readline() instead of raw recv() calls
        self.data = self.rfile.readline().strip()
        print "{} wrote:".format(self.client_address[0])
        print self.data

        # Likewise, self.wfile is a file-like object used to write back
        # to the client
        self.wfile.write('123')

if __name__ == "__main__":
    HOST, PORT = "localhost", 1234

    # Create the server, binding to localhost on port 1234
    server = SocketServer.TCPServer((HOST, PORT), CommandTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

I have a client written in lua with async

async = require 'async'
for i=1,10 do
    async.tcp.connect({host='127.0.0.1', port=1234}, function(client)
        -- Write something
        client.write('hello there .. ')

        -- Callbacks
        client.ondata(function(chunk)
            print('received: ' .. chunk)
        end)

        -- Done:
        client.onend(function()
            print('connection closed...')
        end)
        async.setTimeout(1000, function()
            print('timed out')
            client.close()
        end)
    end)

    async.go()
end
print('done')

I ran the python server and then the lua client. The client never receives anything and times out all 10 times, while the server got 'hello there' all 10 times.

$> th tcp-client.lua                                                                                                                           
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
timed out                                                                                                                                      
done

Server log:

127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..                                                                                                                                 
127.0.0.1 wrote:                                                                                                                               
hello there ..            

eulerreich avatar Oct 11 '15 03:10 eulerreich

The client never receives anything

According to the Python doc:

the readline() call [...] will call recv() multiple times until it encounters a newline character.

So you should:

  1. insert a newline character: client.write('hello there .. \n'),
  2. get rid of the setTimeout section since the server closes the connect right after.

deltheil avatar Oct 11 '15 14:10 deltheil