在wordpress中运行python脚本
-
-
如果这是一个非常简单的脚本,我想我会用PHP作为WordPress插件/模板重写它;-)但是在某些情况下,人们使用iframe嵌入外部页面.If it's a very simple script, I think I would just rewrite it in PHP as a WordPress plugin/template ;-) But in some cases people use iframes to embed external pages.
- 1
- 2013-10-27
- birgire
-
直接iframe吗?:]iframe it directly? :]
- 0
- 2013-10-27
- Jesse
-
这仅仅是偶然,还是您的python代码确实与PHP混合在一起?Is this just an accident, or is your python code really mixed with PHP?
- 0
- 2013-11-05
- fuxia
-
我粘贴了终端跟踪,并通过"更多"命令显示了文件...将整理一些...I pasted the terminal trace, with the files being displayed by the 'more' command... will tidy up a little...
- 0
- 2013-11-05
- Joe
-
3 个回答
- 投票数
-
- 2013-10-27
您可以使用
popen()
来阅读或写入Python脚本(这也适用于任何其他语言).如果您需要交互(传递变量),请使用proc_open()
.在WordPress插件中打印 Hello World!的简单示例
创建插件,注册简码:
<?php # -*- coding: utf-8 -*- /* Plugin Name: Python embedded */ add_shortcode( 'python', 'embed_python' ); function embed_python( $attributes ) { $data = shortcode_atts( [ 'file' => 'hello.py' ], $attributes ); $handle = popen( __DIR__ . '/' . $data['file'], 'r' ); $read = ''; while ( ! feof( $handle ) ) { $read .= fread( $handle, 2096 ); } pclose( $handle ); return $read; }
现在,您可以在帖子编辑器中将该短代码与
[python]
或[python file="filename.py"]
一起使用.将要使用的Python脚本放入与插件文件相同的目录中.您还可以将它们放入目录中,并在简码处理程序中调整路径.
现在创建一个像这样的复杂Python脚本:
print("Hello World!")
仅此而已.使用简码,并获得以下输出:
You can use
popen()
to read or write to a Python script (this works with any other language too). If you need interaction (passing variables) useproc_open()
.A simple example to print Hello World! in a WordPress plugin
Create the plugin, register a shortcode:
<?php # -*- coding: utf-8 -*- /* Plugin Name: Python embedded */ add_shortcode( 'python', 'embed_python' ); function embed_python( $attributes ) { $data = shortcode_atts( [ 'file' => 'hello.py' ], $attributes ); $handle = popen( __DIR__ . '/' . $data['file'], 'r' ); $read = ''; while ( ! feof( $handle ) ) { $read .= fread( $handle, 2096 ); } pclose( $handle ); return $read; }
Now you can use that shortcode in the post editor with
[python]
or[python file="filename.py"]
.Put the Python scripts you want to use into the same directory as the plugin file. You can also put them into a directory and adjust the path in the shortcode handler.
Now create a complex Python script like this:
print("Hello World!")
And that’s all. Use the shortcode, and get this output:
-
正确答案忽略了python脚本的第一行,至少就我而言,至少需要是#!/usr/bin/envpythonCorrect answer omits that first line of the the python script, at least in my case, needs to be #!/usr/bin/env python
- 0
- 2014-05-19
- MikeiLL
-
@MikeiLL这取决于用户的系统,因此我故意将其省略.@MikeiLL That depends on the user’s system, so I left it out deliberately.
- 1
- 2014-05-20
- fuxia
-
基本上是一个安全漏洞.如果您可以通过管道传输到python,也可以通过管道传输到任何其他进程,这可以用于升级更多琐碎的漏洞利用.basically making a security hole. If you can pipe to python, you can pipe to any other process as well and this can be used to escalate any more trivial exploit.
- 1
- 2018-08-15
- Mark Kaplun
-
@MarkKaplun是的,这不是一个好主意.要做到这一点,必须要进行命令转义,然后进行JavaScript + PHP转义.除非有非常特定的理由,否则这不是在WordPress中开发任何内容的好方法."您的科学家-程序员非常关注是否可以,他们没有停下来思考是否应该这样做."@MarkKaplun yes, this is not a good idea. To do this "right" there will have to be command escaping going in, and JavaScript+PHP escaping going on. This is not a good way to develop anything inside WordPress, unless there is a VERY specific reason to do this. "Your -scientists- programmers were so preoccupied with whether or not they could, they didn’t stop to think if they should."
- 0
- 2020-01-20
- Brian Stinar
-
- 2016-10-06
我遵循第一个答案中的示例脚本,但是没有输出或错误.
我更改了这一行:
$handle = popen( __DIR__ . '/' . $data['file'], 'r' );
对此:
$handle = popen( __DIR__ . '/' . $data['file'] . ' 2>&1', 'r' );
,然后收到"权限被拒绝"消息.
在控制台上,我运行
chmod 777 hello.py
刷新页面,一切正常.
这可能是乔在上面看到的问题.对不起,我没有足够的代表发表评论.希望这对某人有帮助.
I followed the example script from the first answer, but was getting no output or errors.
I changed this line:
$handle = popen( __DIR__ . '/' . $data['file'], 'r' );
to this:
$handle = popen( __DIR__ . '/' . $data['file'] . ' 2>&1', 'r' );
and then got a "permission denied" message.
On the console, I ran
chmod 777 hello.py
refreshed the page, and everything worked perfectly.
This may be the issue Joe was seeing above. I don't have enough rep to make a comment, sorry. Hope this helps someone.
-
不要设置权限777.只需使其成为可执行文件即可.chmod + xfilename.py将起作用Do not make the permission 777. Just make it executale. `chmod +x filename.py` will do
- 0
- 2019-06-25
- Dheeraj M Pai
-
- 2014-05-19
下面是一个小脚本,它如上所述使用
proc_open
将一个简单的文本变量发送到python脚本:add_shortcode( 'execute_python', 'execute_python_with_argv' ); function execute_python_with_argv( $attributes ){ $description = array ( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ); $application_system = "python "; $application_path .= plugin_dir_path( __FILE__ ); $application_name .= "hello.py"; $separator = " "; $application = $application_system.$application_path.$application_name.$separator; $argv1 = '"output to receive back from python script"'; $pipes = array(); $proc = proc_open ( $application.$argv1 , $description , $pipes ); //echo proc_get_status($proc)['pid']; if (is_resource ( $proc )) { echo "Stdout : " . stream_get_contents ( $pipes [1] ); //Reading stdout buffer fclose ( $pipes [1] ); //Closing stdout buffer fclose ( $pipes [2] ); //Closing stderr buffer $return_value = proc_close($proc); echo "<br/>command returned: $return_value<br/>"; } $application_test = glitch_player_DIR.$application_name; echo "<br/>Is ".$application_test." executable? ".is_executable($application_test)." "; echo "readable? ".is_readable($application_test)." "; echo "writable? ".is_writable($application_test)." "; } //EOF main/shortcode function
在底部添加了一些测试,以查看python文件是否为
rwx
.我认为发送argv
的一种更好的方法是使用fwrite,但是在本教程.这是我使用的python脚本.如上所述,根据服务器,可能需要
#!/usr/bin/env python
之类的东西.#!/usr/bin/env python from sys import argv script, what_he_said = argv print "This is what you submitted: %s \n \n Isn't that amazing, man? " % what_he_said
Here's a little script that uses
proc_open
as noted above, to sent one simple text variable to a python script:add_shortcode( 'execute_python', 'execute_python_with_argv' ); function execute_python_with_argv( $attributes ){ $description = array ( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ); $application_system = "python "; $application_path .= plugin_dir_path( __FILE__ ); $application_name .= "hello.py"; $separator = " "; $application = $application_system.$application_path.$application_name.$separator; $argv1 = '"output to receive back from python script"'; $pipes = array(); $proc = proc_open ( $application.$argv1 , $description , $pipes ); //echo proc_get_status($proc)['pid']; if (is_resource ( $proc )) { echo "Stdout : " . stream_get_contents ( $pipes [1] ); //Reading stdout buffer fclose ( $pipes [1] ); //Closing stdout buffer fclose ( $pipes [2] ); //Closing stderr buffer $return_value = proc_close($proc); echo "<br/>command returned: $return_value<br/>"; } $application_test = glitch_player_DIR.$application_name; echo "<br/>Is ".$application_test." executable? ".is_executable($application_test)." "; echo "readable? ".is_readable($application_test)." "; echo "writable? ".is_writable($application_test)." "; } //EOF main/shortcode function
Added a few tests as the bottom to see if the python file is
rwx
. I think a better way to send theargv
would be using fwrite, but it wasn't working for me following this tutorial.Here is the python script I used. As noted in comments above, something like
#!/usr/bin/env python
may be necessary, depending on server.#!/usr/bin/env python from sys import argv script, what_he_said = argv print "This is what you submitted: %s \n \n Isn't that amazing, man? " % what_he_said
我为个人博客安装了WordPress,并且逐步将我多年来写的所有小网页内容移植到博客页面上.
这样的页面是 http://www.projecttoomanycooks.co. uk/cgi-bin/memory/majorAnalysis.py 是一个简单的python脚本,可返回单词列表-我想将这种行为嵌入到wordpress页面中-有人可以为我指出正确的方向在wordpress中运行python点的简单方法?
编辑-在下面的精彩回答之后,我得到了很多建议...但是不幸的是,我还没有...
我有在服务器上执行的python ...
,它与已激活的插件在同一目录中.
python代码...,其中包含以下代码...
php: