duanjiao3686 2013-06-20 19:35
浏览 84
已采纳

PHP执行时,Python SSH脚本不起作用

I have a Python (2.7) script that connect to a remote device through SSH and configure it. This script works fine when I run it.

I have a PHP page that execute other Python scripts and they are also working fine. The problem: the SSH script doesn't work when I'm running it from the PHP page. (there is no problem with the PHP page nor the Python script)

To be more specific, the script (when executed by PHP) is running until it gets to the first SSH related code (import paramiko).

How can it be, and is there anything I can do to make it work? Thanks!

(OS: windows XP. Server:Wamp (Apachi http server)

for example, consider this simple code:

fp=open("file.txt","w")
fp.write("text")
import paramiko
fp.write("another text")
fp.close()

if executed by me, everything works fine. If by the PHP page- only the code until "import paramiko" is executed.

or:

fp=open("file.txt","w")
fp.close()
import paramiko
fp2=open("file2.txt","w")
fp2.close()

Executed by me- both files created. by php- only the first.

Moreover: I tried using Try & Catch but nothing comes up. No exception is thrown.

  • 写回答

1条回答 默认 最新

  • dongmieqiao3152 2013-06-20 20:15
    关注

    My guess is that paramiko is not in your PYTHONPATH environmental variable for the Apache user.

    When Apache fires up PHP, it is calling PHP as the whatever the "Apache" user happens to be (in Linux normally apache (or httpd) user in group www-data). Sometimes this user will have different environment variables set than the ones that users normally have. While this generally doesn't matter, sometimes it will cause bizarre interactions (and I can see it happening with Pip).

    I would add a try... except around paramiko:

    try:
       import paramiko
    except Exception as e:
       # log your exception here.
       pass # I put this here so the example can compile.
    #rest of the script
    

    That should let you know whether or not it is installed (and I suspect it isn't).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题