【发布时间】: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.pyBTW)。