Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read from python TCP server #23

Open
eulerreich opened this issue Oct 11, 2015 · 1 comment
Open

Can't read from python TCP server #23

eulerreich opened this issue Oct 11, 2015 · 1 comment

Comments

@eulerreich
Copy link

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 ..            
@deltheil
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants