【问题标题】:How to make python script work on Apache如何使 python 脚本在 Apache 上运行
【发布时间】:2014-11-04 02:24:42
【问题描述】:

我有以下脚本,如何使它在 apache 上工作(我安装了 python 服务器和所有必需的配置),现在我的问题是我在哪里添加 def index(): 我得到缩进错误:(,你有这个解决方案问题?以及如何改变:

port = 22
user = "user"
password = "password"
host = "127.0.0.1"

$_GET ? (http://localhost/test.py?host=127.0.0.1&port=22&user=test&password=123)

import paramiko
import sys, os
import socket
import re

# - - - - - - - - - - - - - - - - #
#          SSH Checker       #
# - - - - - - - - - - - - - - - - #
#def index():


def is_work_sshd(host, dPort=22):
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(20) 
                try:
                                sock.connect((host, dPort))
                except:
                                return 1
                sock.close()
                return 0


def check_server(host, user, password, port=22):
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                #proxy = paramiko.ProxyCommand("127.0.0.1:8118")
                if is_work_sshd(host,port): return 2
                try:
                                ssh.connect(host, username=user, password=password, port=port)
                                ssh.close()
                except:
                                return 1
                return 0

def index():
                port = 22
                user = "user"
                password = "password"
                host = "127.0.0.1"

                ret = check_server(host, user, password, port)
                if not ret:
                                return "CONNECT"
                elif ret == 1:
                                return "FAILED"
                else:
                                return "FAILED"

错误信息:

root@www:/var/www# python t.py
  File "t.py", line 49
    ret = check_server(host, user, password, port)
                                                 ^

【问题讨论】:

  • 这似乎是关于 Python 缩进的问题,根本不是关于 Apache 的问题。
  • Python 从根本上不同于 PHP(您的代码和假设暗示您来自 PHP)。我建议你看看Flask 以及如何integrate it with Apache
  • SHOW 您如何将def index 添加到代码中以获取错误,不要只是告诉我们您添加了错误的缩进代码并期望我们来猜测。另外,请阅读 PEP 8; 16 个空格的缩进是荒谬的。
  • 我编辑了它,看看我是如何添加 def index 的 :)
  • 问题正是 Python 告诉你的:expected an indented block。你把一个函数定义放在那里没有函数体。 (这也不适用于python test.py BTW)。

标签: python apache ubuntu


【解决方案1】:

您应该在port = 22 行之前添加def index():

                return 0

def index():
    return "<html><body>Hello, world.</body></html>"

port = 22

注意:在 Python 中,缩进是语法的一部分。因此,请确保您正确正确地缩进。另见https://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming

确保您使用的代码编辑器始终使用制表符或空格进行缩进,并且不要将两者混用。显示空白的编辑器效果很好,自动清理 Python 代码缩进的编辑器更好。

编辑这是正确的代码:

def check_server(host, user, password, port=22):
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                #proxy = paramiko.ProxyCommand("127.0.0.1:8118")
                if is_work_sshd(host,port): return 2
                try:
                                ssh.connect(host, username=user, password=password, port=port)
                                ssh.close()
                except:
                                return 1
                return 0

def index():
                port = 22
                user = "user"
                password = "password"
                host = "127.0.0.1"
                ret = check_server(host, user, password, port)

                if not ret:
                                return "CONNECT"
                elif ret == 1:
                                return "FAILED"
                else:
                                return "FAILED"

【讨论】:

  • 我在port = 22行之前添加了它,但仍然得到IndentationError: expected an indented block
  • 然后阅读并尝试理解我的答案的第二部分。发布损坏的代码也会有所帮助。
  • 什么编辑器可以为python自动清理缩进?因为实际上我使用的是notepad++
  • 您是否切换到 Python 模式并“显示空白”?一个相当不错的 Python 编辑器是 Sublime Text 3pep8 plugin
  • 我也有!我从port = 22 到其余代码中投入了多少?实际上我把 def index(): 没有空格(在冒号 1 中):)
猜你喜欢
  • 1970-01-01
  • 2019-02-16
  • 2015-02-18
  • 2014-09-13
  • 2019-05-14
  • 2021-02-28
  • 2020-03-05
  • 2011-04-13
  • 1970-01-01
相关资源
最近更新 更多