如何在Python-CGI脚本中获取python变量中javascript变量的值

时间:2016-12-21 23:59:46

标签: javascript python html cgi

我想将javascript变量的值带入python变量。

这是Python-CGI脚本,它显示一个选择框,页面上显示所选值。但我想在python变量中选择值。

#!/usr/bin/python2.7 -u
import time, sys, os

# Import modules for CGI handling
import cgi, cgitb

sys.stdout.write('Content-Type: text/html;charset=utf-8\n')
print "\n\n"
print "<html>"
print "<body>"

rows = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
print "<script>"
print "function showData() {"
print "var sel = document.getElementById('combo');"
print "var value = sel.value;"
print "var text = sel.options[sel.selectedIndex].text;"
print "document.getElementById('secondP').innerHTML=text;}"
print "</script>"

print '<form name="comboform">'
print '<select name="comboselect" onchange="showData()" id="combo">'
for row in rows:
    print "<option value='%s'>%s</option>"%(row,row)
print "</select>"
print "</form>"

print '<p id="secondP">&nbsp;</p>'

# HERE I WANT TO TAKE VALUE OF SELECTED IP ADDRESS (var value) IN PYTHON VARIABLE
# selected_ip_adr = value #something like this where value has selected ip address
# NEED TO PASS THE SAME TO ANOTHER SCRIPT

sys.stdout.flush()
print "</body>"
print "</html>"

如果有人可以提出解决方法来实现这一目标,那将会很棒。

1 个答案:

答案 0 :(得分:0)

我能够使用以下代码和多个页面来解决它:

# Create instance of FieldStorage
form = cgi.FieldStorage()
op = form.getvalue('combo')