最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Executing .py scripts from functions.php

programmeradmin7浏览0评论

I would like to run and store the value returned from a python script, in response to certain admin actions.

I've tried a bunch of suggested solutions but the only method that has worked for me is:

$python_script_path = get_template_directory() . '/hello.py';

$python_command = 'C:\Users\fluxian\AppData\Local\Microsoft\WindowsApps\python.exe ' . escapeshellarg($python_script_path);

ob_start();
passthru($python_command . ' dog');
$output = ob_get_contents();
ob_end_clean();

echo $output; // Hello dog

Which seems dirty, but if it works it works. I don't want to hard-code the location to a python install though. Is there a way to figure out where python is actually installed dynamically, instead of relying on hard-coding its location?

Whenever I run shell_exec or exec I get an error saying that python is not a recognised command. I assume the shell php is calling is not the cmd that Windows is exposing? Fixing that seems like a neater solution, if anyone knows how.

I would like to run and store the value returned from a python script, in response to certain admin actions.

I've tried a bunch of suggested solutions but the only method that has worked for me is:

$python_script_path = get_template_directory() . '/hello.py';

$python_command = 'C:\Users\fluxian\AppData\Local\Microsoft\WindowsApps\python.exe ' . escapeshellarg($python_script_path);

ob_start();
passthru($python_command . ' dog');
$output = ob_get_contents();
ob_end_clean();

echo $output; // Hello dog

Which seems dirty, but if it works it works. I don't want to hard-code the location to a python install though. Is there a way to figure out where python is actually installed dynamically, instead of relying on hard-coding its location?

Whenever I run shell_exec or exec I get an error saying that python is not a recognised command. I assume the shell php is calling is not the cmd that Windows is exposing? Fixing that seems like a neater solution, if anyone knows how.

Share Improve this question asked Mar 26 at 13:36 FluxianFluxian 1829 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Which seems dirty, but if it works it works. I don't want to hard-code the location to a python install though. Is there a way to figure out where python is actually installed dynamically, instead of relying on hard-coding its location?

There's several options:

  1. Put the python executable in PATH so that the path is unnecessary and it can be shortened to just python ...args..
  2. Pass the path in an environment variable, but this requires pre-configuring at a server level
  3. Pass it via a config file outside of the public dir, again requires server specific file
  4. Query the python file via a HTTP request and rely on a python web server to execute your query
  5. You may be tripped up by the working directory, which might change depending on how and where you make the request to WordPress.
  6. Read in the return code from python, right now there is no error handling code

Note though that executing shell scripts is one of the most dangerous things you can do and these functions are explicitly disabled on some hosts for that reason. This code will be flagged by most code quality/security scanning tools as a result.

Some further notes:

  • Move your python application outside your theme, ideally outside your servers publicly accessible folders, otherwise someone could hit that python URL directly and execute/read/download the contents
  • Assemble your command in 1 step so that it can be escaped all at once, don't append extra arguments afterwards

Whenever I run shell_exec or exec I get an error saying that python is not a recognised command. I assume the shell php is calling is not the cmd that Windows is exposing? Fixing that seems like a neater solution, if anyone knows how.

At this point you've moved beyond the world of WordPress and have a generic PHP question. But the answer depends enormously on how your server is set up. E.g. if you're using a docker based environment then that path won't work and you'll need to make sure a python install is present inside the container. If you're using a tool such as WP Studio or wp-now then there is no python ( and no PHP either! That tool transpiles it all into javascript ). Likewise if your local environment relies on WSL it will have its own filesystem etc.. It may even be that Windows security is preventing access, but you would need to ask someone more experienced with modern Windows security practices on how to check for that.

As for passthru versus exec vs system this stackoverflow question handles that: https://stackoverflow/questions/732832/php-exec-vs-system-vs-passthru#732844

发布评论

评论列表(0)

  1. 暂无评论