plugins - Python in Wordpress - A "Hello World" implementation

I've read this question (and many more web pages) in order to figure out how to run a simple python script via a Wo

I've read this question (and many more web pages) in order to figure out how to run a simple python script via a Wordpress plugin.

However, I couldn't do it: I always get a "Blank" output.

No errors were displayed.

How can I execute a python function via Wordpress plugin?

I've read this question (and many more web pages) in order to figure out how to run a simple python script via a Wordpress plugin.

However, I couldn't do it: I always get a "Blank" output.

No errors were displayed.

How can I execute a python function via Wordpress plugin?

Share Improve this question asked Aug 15, 2018 at 5:03 nickhnickh 1051 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I tried it step by step and finally succeeded.

Step 1 - hello.py

I tried to create the hello.py file with Atom on Windows 10 and uploaded it to a Linux server for many times. It could run with the "python3 hello.py" command, but always failed to run when using the "./hello.py" command.

I had to create it on the Linux server and the "./hello.py" command worked.

# pwd
/var/www/html/wordpress/wp-content/plugins/run-python
# cat hello.py 
#!/usr/bin/env python3
print("Hello")
# chmod +x hello.py
# ./hello.py 
Hello

Step 2 - t-popen.py

I created the t-popen.py file with Atom on Windows 10 and uploaded it to the Linux server. This PHP file was used to make sure the popen() function worked.

# cat t-popen.php 
<?php

/* Add redirection so we can get stderr. */
$handle = popen( __DIR__ . '/hello.py', 'r' );
$read   = '';

while ( ! feof( $handle ) ) {
    $read .= fread( $handle, 2096 );
}

pclose( $handle );

var_dump( $read );
echo $read;
# php t-popen.php 
string(6) "Hello
"
Hello

Step 3 - The WP plugin

I created the run-python.php file with Atom on Windows 10 and uploaded it to the Linux server.

<?php
/**
 * Try - Run Python
 *
 * @package     Try\Run Python
 *
 * Plugin Name: Try - Run Python
 * Plugin URI:
 * Description: Try to run a Python script in the WordPress plugin.
 * Version:     1.0
 * Author:      Box
 * Author URI:
 */

add_shortcode( 'python', 'embed_python' );

function embed_python( $attributes ) {
    $data = shortcode_atts(
        array(
            'file' => 'hello.py',
        ),
        $attributes
    );

    $handle = popen( __DIR__ . '/' . $data['file'], 'r' );
    $read   = '';

    while ( ! feof( $handle ) ) {
        $read .= fread( $handle, 2096 );
    }

    pclose( $handle );

    return $read;
}

Step 4

I activated the plugin and tried to add a shortcode as [python] to a post.

The "hello" string showed in the content when I viewed the post.

Note

The 3 files mentioned above are all located in the same directory which is "/var/www/html/wordpress/wp-content/plugins/run-python" in this case.

You should not be able to run any script on your server from your PHP. This is a security nightmare you should avoid. If you just have to be able to communicate with some random script on your server you should restrict the ability to only trusted computers (best thing use a different site which uses a different php.ini for that) or find a roundabout way to do that like using write to a file monitored by the python script to pass parameters. (open socket in your python can also be an option).

If as you say in the comments you need to process the output on your local machine, just use a wget or whatever is its python equivalent to get the output from the site and then process it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742314124a4420492.html

相关推荐

  • plugins - Python in Wordpress - A &quot;Hello World&quot; implementation

    I've read this question (and many more web pages) in order to figure out how to run a simple python script via a Wo

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信