2011-05-29 96 views
1

我有一個在命令行上運行的Python程序。它使用raw_input()從用戶的鍵盤讀取一個字符串。我想讓該程序在Google App Engine上可用,並且想要使用App Engine Shell,因爲它具有命令提示符。Google App Engine shell中的Python raw_input

但是,shell似乎提供了一個「假」提示,並且當我在我的程序中使用raw_input()時,它只是返回EOF。

你有什麼提示而不是raw_input(),或者使交互式控制檯python應用程序可用的替代方法嗎? (它沒有被看中詛咒的東西或什麼,只是讀一個緩衝,字符串的東西。)

編輯:該計劃是一個在線冒險像魔域http://thcnet.net/error/index.php

回答

1

貌似在App Engine shell不會將stdin綁定到用於交換命令和結果的瀏覽器的AJAX連接。換句話說,你不能用它來實現你的目標。

而不是通過Web公開命令行界面(這聽起來不是一個好主意),我會創建一個簡單的基於表單的包裝器,它作爲底層命令行程序的前端。

2

該應用程序的Python源代碼是Google Code上的available,供您學習或重複使用。由於安全原因,raw_input()可能已被禁用,並始終返回EOF。

這個shell使用AJAX接口,只是從輸入區域獲取代碼並解析它。請參閱存儲庫中的shell.js

/** 
* This is the prompt textarea's onkeypress handler. Depending on the key that 
* was pressed, it will run the statement, navigate the history, or update the 
* current statement in the history. 
* 
* @param {Event} event the keypress event 
* @return {Boolean} false to tell the browser not to submit the form. 
*/ 
shell.onPromptKeyPress = function(event) { 
    var statement = document.getElementById('statement'); 

    if (this.historyCursor == this.history.length - 1) { 
    // we're on the current statement. update it in the history before doing 
    // anything. 
    this.history[this.historyCursor] = statement.value; 
    } 

    // should we pull something from the history? 
    if (event.ctrlKey && event.keyCode == 38 /* up arrow */) { 
    if (this.historyCursor > 0) { 
     statement.value = this.history[--this.historyCursor]; 
    } 
    return false; 
    } else if (event.ctrlKey && event.keyCode == 40 /* down arrow */) { 
    if (this.historyCursor < this.history.length - 1) { 
     statement.value = this.history[++this.historyCursor]; 
    } 
    return false; 
    } else if (!event.altKey) { 
    // probably changing the statement. update it in the history. 
    this.historyCursor = this.history.length - 1; 
    this.history[this.historyCursor] = statement.value; 
    } 

    // should we submit? 
    if (event.keyCode == 13 /* enter */ && !event.altKey && !event.shiftKey) { 
    return this.runStatement(); 
    } 
}; 
0

我通過將程序轉換爲生成器來解決了該問題。

示例代碼可以在https://github.com/larsr/consoleapp

找到你可以嘗試一下這裏http://pyconsoleapp.appspot.com/

的程序存儲在prog.py,不得不被修改了一點;用產量替代raw_input(),用修改後的打印替換打印。 App Engine處理程序將來自HTML表單的輸入發送到generate.send(輸入)生成器,該輸出由yield語句「返回」。

while True: 
    print "What's your name?" 
    name = raw_input() 
    print "Hello "+name+"!" 
    print 

必須被修改成

from appconsole import myprint, printoutput 

def prog_gen(namn=""): 

    while True: 
     myprint("What's your name?") 
     name = yield printoutput() 
     myprint("Hello "+name+"!") 
     myprint()