python xinetd client not receiving data

2019-07-23 13:03发布

问题:

I'm trying to use xinetd to remotely run a command (traccejob). When I connect through telnet, everything works fine. Unfortuantely, the client that I've written doesn't seem to receive the data from the server. The code looks like:

server:

import sys
import commands

def main():
        tjinput = sys.stdin.readline().strip()
        (ret, out) = commands.getstatusoutput('/usr/bin/tracejob '+tjinput)
        print out
        sys.stdout.flush()

if __name__ == "__main__":
        main()

client:

host = 'xxx.xxx.xxx.xxx'
port = 12345

import socket
import sys


def main(argv):
        message = 'hello'
        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sock.connect((host, port))
        sock.send(message)
        data = sock.recv(1024)
        sock.close()
        print repr(data)

if __name__ == '__main__':
        main(sys.argv)

The client process stops on the sock.recv(1024) line. I can't for the life of me tell why sock.recv isn't reading the output from the socket. Probably a coding issue? If it helps, the xinetd.d file looks like this:

service tracejob
{
        flags                   = IPv4
        disable                 = no
        socket_type             = stream
        wait                    = no
        user                    = root
        group                   = root
        server                  = /usr/local/bin/tracejob_xinetd.py
        port                    = 12345
        type                    = UNLISTED
}

where tracejob_xinetd.py is the server described above.

Any tips? Thanks in advance.

回答1:

You have a deadlock situation: The client sends an incomplete line and waits for the server to send something, the server waits for line completion or EOF before it sends a reply.

So you have now 2 ways to proceed now:

  1. Append a \n to the string being sent.
  2. "Half-close" the socket on client side with sock.shutdown(socket.SHUT_WR) after writing, but before reading.


标签: python xinetd