How do I execute Python script in PHP?

Source: Internet
Author: User
PHP Python SSH2

You want to SSH to the remote server via PHP, then execute a python file on the server, get the results, and print from PHP.
My code is as follows:

$connection = Ssh2_connect (' Remote-server ', $); Ssh2_auth_password ($connection, ' root ', ' pwd '); $string = Ssh2_exec ($ Connection, ' Python pythonfile.py; Sleep '); Stream_set_blocking ($string, true); Echo stream_get_contents ($string);


But the null value is printed in PHP.
If I put
$string = ssh2_exec ($connection, ' Python pythonfile.py; sleep 20 ')
Replaced by
$string = ssh2_exec ($connection, ' ls-l ')
There is no problem, the program output normally.

The python file on the remote server typically takes 15 seconds to run, so I set sleep 20, but I still get no results.
Want to ask if the code is wrong? How to correct it?


Reply to discussion (solution)

Why do you sleep? Does the far end sleep make sense?

Remove sleep because stream_set_blocking ($string, True) is set to block mode, which will wait for the shell to execute before it gets the result of execution.

Remove sleep because stream_set_blocking ($string, True) is set to block mode, which will wait for the shell to execute before it gets the result of execution.

The issue in the post has been resolved, the return is empty because the path to the Python file is not correct, Python path/to/python/file, I then added a/in front of the path, so the program cannot find the file to execute.

Now the program can return the results I want, but there is a small problem, I hope you can help solve, you can add points.
$string = ssh2_exec ($connection, ' CD a/b/c; python discovery.py >/root/output.txt; ');
Stream_set_blocking ($string, true);
Sleep (20)

$stream = fopen ("ssh2.sftp://$sftp/root/output.txt", ' R ');
$contents = Fread ($stream, FileSize ("ssh2.sftp://$sftp/root/output.txt"));
Echo $contents;
Fclose ($stream);

This is my current code, through remote SSH, I first go to the C folder, then execute the discovery.py file from it, output the results to the root of the output.txt.
But as I said above, the execution of this Python file usually takes 15 seconds to return the result, so it must be added a sleep (20), otherwise the program will say "fopen (): Unable to open ssh2.sftp://resource ID # 5/root/output.txt on remote host in/var/www/html/test/ssh.php in line 20 "I guess it's because the time is too short that the program hasn't been able to generate the" output.txt "file.

But Hnxxwyq, if, as you say, stream_set_blocking () will wait until the shell finishes executing to get the result, why did the output file not generate the program already executed to fopen?
Although now the code can get the results I want, but I think that manually set sleep () is not the optimal method, because you can not correctly estimate the time required for output.txt generation, if 15 seconds to generate a result file, and I let the Program sleep (20), then wasted time, So is there a way to get the program to execute fopen () immediately after the output.txt is generated?
Seemingly stram_set_blocking () does not have this function: I'm not too familiar with PHP, gentlemen.

solved, I changed the code again
$stream = ssh2_exec ($connection, ' CD dca_automation/main/qautilities/utils ; Python cluster_discovery.py ');
Stream_set_blocking ($stream, true);

$string = stream_get_contents ($stream);
Echo $string;

no longer outputs the Python results to a file, so that $stream gets it directly, because "in non-blocking mode a fgets () call would always return right away and in Blo cking mode It will wait for data to become available on the stream ", so if you use the previous code, once the Output.txt file is generated, even if the result has not been written up, $stream has obtained several Stream_set_blocking () will automatically terminate. But if I write the result of the python directly onto the stream, the result is not fully generated before the process will block.
I don't know what I'm explaining, right? The inappropriate place welcomes everyone to correct me.

Solved, I changed the code again.
$stream = ssh2_exec ($connection, ' CD dca_automation/main/qautilities/utils; python cluster_discovery.py ');
Stream_set_blocking ($stream, true);

$string = Stream_get_contents ($stream);
Echo $string;

No longer output Python results to a file, let $stream get it directly, because "in non-blocking mode a fgets () call would always return right away while in blocking mode It will wait for data to become available on the stream ", so, if the previous code, once the Output.txt file generation, even if the results have not been written up, $stream have obtained data, so Strea M_set_blocking () will automatically terminate. But if I write the result of the python directly onto the stream, the result is not fully generated before the process will block.
I don't know what I'm explaining, right? The inappropriate place welcomes everyone to correct me.

No mistake, it is the stream that receives the data returned by the shell, triggering the stream to return the result, thus executing the stream_get_contents

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.