Looping Fget with fsockopen in PHP 5.x

I have a Python Server finally working and responding to several commands with output, however I now have a problem with PHP receiving the full output. I tried commands like fgets, fread, the only command that seems to work is "fgets".

However, this only works in the data row, after which I created a while statement, shown below:

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

However, it seems that the Python server is not sending Feof at the end of the output, so the php page is not working and does not display anything. As I said above, it just launches echo fgets ($ handle), it works fine and displays one line, again running the command under nothing displays the next line etc

I have attached an important part of my Python Script below:

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 also added a full PHP script:

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

Thanks Ashley

+3
1

, . while. , , feof true.

+ " &". , , . , & .

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()

, while-loop php script. fgets .

+1

Source: https://habr.com/ru/post/1710950/


All Articles