<tfoot id='NSRfk'></tfoot>
  • <legend id='NSRfk'><style id='NSRfk'><dir id='NSRfk'><q id='NSRfk'></q></dir></style></legend>
    • <bdo id='NSRfk'></bdo><ul id='NSRfk'></ul>

      <small id='NSRfk'></small><noframes id='NSRfk'>

    1. <i id='NSRfk'><tr id='NSRfk'><dt id='NSRfk'><q id='NSRfk'><span id='NSRfk'><b id='NSRfk'><form id='NSRfk'><ins id='NSRfk'></ins><ul id='NSRfk'></ul><sub id='NSRfk'></sub></form><legend id='NSRfk'></legend><bdo id='NSRfk'><pre id='NSRfk'><center id='NSRfk'></center></pre></bdo></b><th id='NSRfk'></th></span></q></dt></tr></i><div id='NSRfk'><tfoot id='NSRfk'></tfoot><dl id='NSRfk'><fieldset id='NSRfk'></fieldset></dl></div>
      1. 如何从 php 运行 Python 脚本

        时间:2023-10-12
        • <bdo id='P51sF'></bdo><ul id='P51sF'></ul>

            <small id='P51sF'></small><noframes id='P51sF'>

            <i id='P51sF'><tr id='P51sF'><dt id='P51sF'><q id='P51sF'><span id='P51sF'><b id='P51sF'><form id='P51sF'><ins id='P51sF'></ins><ul id='P51sF'></ul><sub id='P51sF'></sub></form><legend id='P51sF'></legend><bdo id='P51sF'><pre id='P51sF'><center id='P51sF'></center></pre></bdo></b><th id='P51sF'></th></span></q></dt></tr></i><div id='P51sF'><tfoot id='P51sF'></tfoot><dl id='P51sF'><fieldset id='P51sF'></fieldset></dl></div>

          1. <tfoot id='P51sF'></tfoot>

                • <legend id='P51sF'><style id='P51sF'><dir id='P51sF'><q id='P51sF'></q></dir></style></legend>
                    <tbody id='P51sF'></tbody>
                • 本文介绍了如何从 php 运行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想从 php 运行 python 脚本.这是我的python代码.保存在/home/pi,文件名为hello.py

                  #!/usr/bin/python进口蓝牙bd_addr="xx:xx:xx:xx:xx:xx"端口=1袜子=bluetooth.BluetoothSocket(bluetooth.RFCOMM)sock.connect((bd_addr.port))数据=""而 1:尝试:数据 +=sock.recv(1024)data_end=data.find('
                  ')如果 data_end!=-1:rec=data[:data_end]打印数据数据=数据[数据结束+1:]除了键盘中断:休息

                  这是我的 php 代码.它保存在/var/www/html 中,文件名为 php.php

                  我在 chrome 中插入 localhost/php.php,它显示

                  -rw-r-r- 1 pi pi 378 Mar 8 12:07/home/pi/hello.py

                  有什么问题??

                  解决方案

                  ls 命令用于列出目录中的文件或获取有关文件的信息.你在你的 python 文件上 ls-ing 并且结果是正确的.它正在为您提供有关文件的信息.

                  只需将文件名放在shell_exec 中,即/home/pi/hello.py.如果您不想依赖 shebang 并且命令 python 在您的 shell 环境中可用,那么您可以使用 python/home/pi/hello.py 而不是裸/home/pi/hello.py.

                  同样,您将变量 datasprint 一起用于您打算使用 data 的地方 - 修复它.

                  <小时><块引用>

                  php 代码:

                  <块引用>

                  或:

                  <块引用>

                  python 代码:

                  #!/usr/bin/python进口蓝牙bd_addr="xx:xx:xx:xx:xx:xx"端口=1袜子=bluetooth.BluetoothSocket(bluetooth.RFCOMM)sock.connect((bd_addr.port))数据=""而 1:尝试:数据 +=sock.recv(1024)data_end=data.find('
                  ')如果 data_end!=-1:rec=data[:data_end]打印数据数据=数据[数据结束+1:]除了键盘中断:休息

                  I want to run python script from php. this is my python code. It is saved in /home/pi and name of file is hello.py

                  #! /usr/bin/python
                  
                  import bluetooth
                  
                  bd_addr="xx:xx:xx:xx:xx:xx"
                  port=1
                  sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
                  sock.connect((bd_addr.port))
                  data=""
                  while 1:
                    try:
                      data +=sock.recv(1024)
                      data_end=data.find('
                  ')
                      if data_end!=-1:
                        rec=data[:data_end]
                        print datas
                        data=data[data_end+1:]
                      except KeyboardInterrupt:
                        break
                  

                  And here is my php code. It is saved in /var/www/html and name of file is php.php

                  <?php
                  $output=shell_exec('ls -l /home/pi/hello.py');
                  echo "<pre>$output</pre>";
                  ?>
                  

                  And I insert localhost/php.php in chrome, it displays

                  -rw-r-r- 1 pi pi 378 Mar 8 12:07 /home/pi/hello.py
                  

                  what is the problem??

                  解决方案

                  ls command is used to list files in a directory or to get information about a file. You are ls-ing on your python file and the result is correct. It is providing you with information about the file.

                  Just put the file name inside of shell_exec that is /home/pi/hello.py. If you do not want to depend on the shebang and the command python is available in your shell environment then you can use python /home/pi/hello.py instead of bare /home/pi/hello.py.

                  Again, you used the variable datas with print where you intended to use data - fix it.


                  php code:

                  <?php
                  $output=shell_exec('python /home/pi/hello.py');
                  echo "<pre>$output</pre>";
                  ?>
                  

                  or:

                  <?php
                  $output=shell_exec('/home/pi/hello.py');
                  echo "<pre>$output</pre>";
                  ?>
                  

                  python code:

                  #! /usr/bin/python
                  
                  import bluetooth
                  
                  bd_addr="xx:xx:xx:xx:xx:xx"
                  port=1
                  sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
                  sock.connect((bd_addr.port))
                  data=""
                  while 1:
                    try:
                      data +=sock.recv(1024)
                      data_end=data.find('
                  ')
                      if data_end!=-1:
                        rec=data[:data_end]
                        print data
                        data=data[data_end+1:]
                      except KeyboardInterrupt:
                        break
                  

                  这篇关于如何从 php 运行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:php exec() 命令权限被拒绝 下一篇:如何使用 PHP 扩展安装 Gearman

                  相关文章

                  1. <small id='nfYaI'></small><noframes id='nfYaI'>

                    <legend id='nfYaI'><style id='nfYaI'><dir id='nfYaI'><q id='nfYaI'></q></dir></style></legend>
                    <i id='nfYaI'><tr id='nfYaI'><dt id='nfYaI'><q id='nfYaI'><span id='nfYaI'><b id='nfYaI'><form id='nfYaI'><ins id='nfYaI'></ins><ul id='nfYaI'></ul><sub id='nfYaI'></sub></form><legend id='nfYaI'></legend><bdo id='nfYaI'><pre id='nfYaI'><center id='nfYaI'></center></pre></bdo></b><th id='nfYaI'></th></span></q></dt></tr></i><div id='nfYaI'><tfoot id='nfYaI'></tfoot><dl id='nfYaI'><fieldset id='nfYaI'></fieldset></dl></div>
                  2. <tfoot id='nfYaI'></tfoot>

                        <bdo id='nfYaI'></bdo><ul id='nfYaI'></ul>