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 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面