python脚本中脚本头的过早结束

时间:2013-01-06 18:41:31

标签: python cgi cgi-bin

我刚刚在Python编写了一个简单的战舰游戏。在终端中,它运行完美,但是当我尝试在线运行它作为cgi-bin脚本时,存在问题。 我的代码是:

#!/usr/bin/python

print "Content-type: text/html\n\n"
print "<html>"

from random import randint

board = []

for x in range(0,5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

def random_row(board):
  return random.randint(0,len(board)-1)

def random_col(board):
  return random.randint(0,len(board[0])-1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = raw_input("Guess Row:")
guess_col = raw_input("Guess Col:")

print ship_row
print ship_col

if (guess_row == ship_row and guess_col == ship_col):
print "Congratulations! You sank my battleship!<br/>"
else:
if board[guess_row][guess_col] == "X":
print "You guessed that one already.<br/>"
if not (0 <= guess_row < len(board)) or not (0 <= guess_col < len(board)):
print "Oops, that’s not even in the ocean.<br/>"
else:
print "You missed my battleship!<br/>"
board[guess_row][guess_col] == "X"
print_board(board)

print "</html>"

我应该提一下,其他简单的hello-world脚本在网页中运行正常。 错误日志根据这个返回这些行:

[Sun Jan 06 20:34:26 2013] [error] [client xxx]   File "
[Sun Jan 06 20:34:26 2013] [error] [client xxx] /usr/lib/cgi-bin/game.py
[Sun Jan 06 20:34:26 2013] [error] [client xxx] ", line
[Sun Jan 06 20:34:26 2013] [error] [client xxx] 34
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx] print "Congratulations! You sank my battleship!<br/>"
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx] ^
[Sun Jan 06 20:34:26 2013] [error] [client xxx] IndentationError
[Sun Jan 06 20:34:26 2013] [error] [client xxx] :
[Sun Jan 06 20:34:26 2013] [error] [client xxx] expected an indented block
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx] Premature end of script headers: game.py

你能帮我解决这个问题吗?

提前致谢!

ps:我是python的新手:D

2 个答案:

答案 0 :(得分:0)

试试这个:

#!/usr/bin/python

print "Content-type: text/html\n\n"
print "<html>"

from random import randint

board = []

for x in range(0,5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print_board(board)

def random_row(board):
    return random.randint(0,len(board)-1)

def random_col(board):
    return random.randint(0,len(board[0])-1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = raw_input("Guess Row:")
guess_col = raw_input("Guess Col:")

print ship_row
print ship_col

if (guess_row == ship_row and guess_col == ship_col):
    print "Congratulations! You sank my battleship!<br/>"
elif board[guess_row][guess_col] == "X":
    print "You guessed that one already.<br/>"
if not (0 <= guess_row < len(board)) or not (0 <= guess_col < len(board)):
    print "Oops, that’s not even in the ocean.<br/>"
else:
    print "You missed my battleship!<br/>"
board[guess_row][guess_col] == "X"
print_board(board)

print "</html>"

答案 1 :(得分:0)

邮政有点旧,但万一有人在看:

尝试运行此程序(从浏览器中)以查看其是否有效:

#!/usr/bin/python
import cgi
cgi.test()

如果没有出现错误,请尝试删除该文件,制作一个新文件并将代码复制/粘贴回来。

我刚刚解决了我的问题。