Skip to content

Can't read from python TCP server #23

@eulerreich

Description

@eulerreich

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 ..            

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions