dongye1934 2014-09-18 06:15
浏览 49

从php调用的python脚本连接到数据库

I have a python script which executes the following line

os.system('mysql -H -uusername -ppwd -hipaddr -e"some query" >filename.html')

when this python script is executed independently everything seems fine, a html file is generated with the query output.

But when i call the python script from php(using shell_exec()) ,there occurs a problem."filename.html" is created but the file is empty.When is searched regarding this i found this has to be done

setsebool -P httpd_can_network_connect=1.

As a result now i can connect to mysql using mysqldb module in python script called from php, which i am not in need of. I want to execute query using execute command of mysql through os.system() which is still a problem when called from php. Is there any suggestions so that i can get it right?

Edit : here is my php script

<?php

$command = escapeshellcmd('python csv_generate.py');
$output = shell_exec($command);
echo $output;
?>

here is my python script (csv_generate.py)

import sys
import os
print "hii"
query="show databases"
os.system('mysql -H -uaaa -pbbb -hhostip -P3306   -e"%s" >filename.html'%(query))

When called from php ,python script gets executed and it prints hii. The html file is also created but the file is empty .Even when i try to display the output in the browser i.e,

os.system('mysql -H -uaaa -pbbb -hhostip -P3306  -e"%s"'%(query))

it justs prints only "hii"

the error log contains

sh: /usr/local/mysql/bin/mysql: Permission denied

  • 写回答

1条回答 默认 最新

  • douwen7475 2015-07-08 22:56
    关注

    You need to make a sh file and put the commands to connect inside.

    For Example, test.sh

    python csv_generate.py
    

    In PHP

    exec("sh test.php");
    

    So it does not fail execute the command

    chmod 777 csv_generate.py
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作