So I have this piece of code in a socket server:

def process(self, request, client_address):
        input_file = request.makefile("rb", 0)
        output = request.makefile('wb', 0)

        remote_input = input_file.readline().strip() + b"\n"
        self.log(remote_input)

        self.proc.stdin.write(remote_input)
        self.proc.stdin.flush()
        self.proc.stdout.flush()

        while True:
            line = self.proc.stdout.readline()
            line = line.decode()
            if line:
                line = bytes(line.rstrip() + "\n", 'utf-8')
                output.write(line)
            else:
                break

Where self.proc is a subprocess with the following setup:

self.proc = subprocess.Popen(
    ["/bin/bash"],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    stdin=subprocess.PIPE)

It seems to work fine for the first time, then breaks afterwards- for example,

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
/bin/bash: line 1: hello: command not found
this doesn't work

It seems to block at the while True loop found in the first snippet. How can I solve this problem?

This thread may help you, especially the linux solution with the socket pair.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.