Question

I have a Python Server finally working and responding to multiple command's with the output's, however I'm now having problem's with PHP receiving the full output. I have tried commands such as fgets, fread, the only command that seems to work is "fgets".

However this only recieve's on line of data, I then created a while statement shown bellow:

 while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    echo $buffer;
}

However it seems the Python server is not sending a Feof at the end of the output so the php page times out and does not display anything. Like I said above, just running echo fgets($handle), work's fine, and output's one line, running the command again under neither will display the next line e.t.c

I have attached the important part of my Python Script bellow:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", port))
s.listen(5)
print "OK."
print "  Listening on port:", port
import subprocess
while 1:
    con, addr = s.accept()
    while True:
        datagram = con.recv(1024)
        if not datagram:
            break
        print "Rx Cmd:", datagram
        print "Launch:", datagram
        process = subprocess.Popen(datagram+" &", shell=True, stdout=subprocess.PIPE)
        stdout, stderr = process.communicate()
        con.send(stdout)
    con.close()
s.close()

I have also attached the full PHP script:

<?php
$handle = fsockopen("tcp://xxx.xxx.xxx.xxx",12345);
fwrite($handle,"ls");
echo fgets($handle);
fclose($handle);
?>

Thanks, Ashley

Was it helpful?

Solution

I believe you need to fix your server code a bit. I have removed the inner while loop. The problem with your code was that the server never closed the connection, so feof never returned true.

I also removed the + " &" bit. To get the output, you need to wait until the process ends anyway. And I am not sure how the shell would handle the & in this case.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", port))
s.listen(5)
print "OK."
print "  Listening on port:", port
import subprocess
try:
    while 1:
        con, addr = s.accept()
        try:
            datagram = con.recv(1024)
            if not datagram:
                continue
            print "Rx Cmd:", datagram
            print "Launch:", datagram
            process = subprocess.Popen(datagram, shell=True, stdout=subprocess.PIPE)
            stdout, stderr = process.communicate()
            con.send(stdout)
        finally:
            print "closing connection"
            con.close()
except KeyboardInterrupt:
    pass
finally:
    print "closing socket"
    s.close()

BTW, you need to use the while-loop in your php script. fgets returns until a single line only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top