簡體   English   中英

jQuery提示給python腳本的變量值

[英]jquery prompted variable value given to python script

我有一個簡單的問題,但我無法解決。 我有一個帶選項的javascript表單,當用戶選擇一個選項時,系統會提示他們輸入如下值:

var integer_value = window.prompt("What is the integer value?", "defaultText")

然后,我需要此整數值供python腳本使用。 因此,如果在我的python腳本中說:

integer_value = 2

它將需要更改為用戶在提示窗口中輸入的任何值。

下面的代碼應滿足您的需求。 可能有更好的方法可以做到這一點,但至少這種方法相當簡單。

下次您遇到Web編程問題時,請告訴我們您正在使用什么服務器,什么框架以及jQuery或Ajax之類的JavaScript。 並發布我們可以實際運行的HTML / JavaScript和Python的代碼的小型工作演示,以便我們可以准確地看到您在說什么。

首先,這是一個小的HTML / JavaScript頁面,提示用戶輸入數據並將其發送到服務器。

send_to_python.html

<!DOCTYPE html>
<html>
<head><title>Send data to Python demo</title>

<script>
function get_and_send_int()
{
    var s, integer_value, default_value = 42;
    s = window.prompt("What is the integer value?", default_value);
    if (s==null || s=='')
    {
        alert('Cancelled');
        return;
    }

    //Check that the data is an integer before sending
    integer_value = parseInt(s);
    if (integer_value !== +s)
    {
        alert(s + ' is not an integer, try again');
        return;
    }

    //Send it as if it were a GET request.
    location.href = "cgi-bin/save_js_data.py?data=" + integer_value;
}
</script>
</head>

<body>
<h4>"Send data to Python" demo</h4>
<p>This page uses JavaScript to get integer data from the user via a prompt<br>
and then sends the data to a Python script on the server.</p>

<p>Click this button to enter the integer input and send it.<br>
<input type="button" value="get &amp; send" onclick="get_and_send_int()">
</p>

</body>
</html>

現在,對於CGI Python程序,該程序將接收數據並將其記錄到文件中。 一個適當的程序將具有一些錯誤檢查和數據驗證,以及一種報告任何錯誤的方式。 Python CGI模塊將使此任務更容易一些,但這不是絕對必要的。

Web服務器通常在名為cgi-bin的目錄中查找CGI程序,通常將其用作程序的“當前工作目錄”。 一個CGI程序不能在普通終端中運行:它的stdin和stdout本質上連接到調用該程序的Web頁面,因此它打印的所有內容都被發送回該Web頁面。 並且(取決於所使用的請求方法)它可以通過stdin從頁面接收數據。

下面的程序不讀stdin(如果嘗試,它將似乎掛起)。 數據作為用於調用程序的URL的一部分發送給它,服務器提取該數據並將其放入CGI程序可以訪問的環境變量中。

save_js_data.py

#! /usr/bin/env python

''' save_js_data

    A simple CGI script to receive data from "send_to_python.html" 
    and log it to a file
'''

import sys, os, time

#MUST use CRLF in HTTP headers
CRLF = '\r\n'

#Name of the file to save the data to.
outfile = 'outfile.txt'

def main():
    #Get the data that was sent from the Web page
    query = os.environ['QUERY_STRING']

    #Get the number out of QUERY_STRING
    key, val = query.split('=')
    if key == 'data':
        #We really should check that val contains a valid integer

        #Save the current time and the received data to outfile
        s = '%s, %s\n' % (time.ctime(), val)
        with open(outfile, 'a+t') as f:
            f.write(s)

    #Send browser back to the refering Web page
    href = os.environ['HTTP_REFERER']
    s = 'Content-type: text/html' + CRLF * 2
    s += '<meta http-equiv="REFRESH" content="0;url=%s">' % href
    print s


if __name__ == '__main__':
    main()

將此程序保存到硬盤驅動器時,請確保已授予其執行權限。 由於您使用的是Flask,因此我假設您的服務器已配置為運行Python CGI程序,並且知道該程序在哪個目錄中查找。

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM