Skip to main content
 首页 » 编程设计

python之无法从 URL 获取参数之为什么我的 cgi.FieldStorage() 是空的

2025年05月04日10pengyingh

我无法从 URL 获取参数。在网上阅读了大量帖子后,但似乎没有任何内容可以回答我的问题...

我有一个简单的 HTML 文件:

<html> 
<head> 
</head>  
<body> 
    <form action="test.py" method="get">             
        Name: <input id="person_name" type="text" name="person_name" >           
        <input id="submit_button" type="submit" value="Submit" >         
    </form>      
</body> 

我的 python 脚本如下所示:

import cgi, cgitb    
 
form = cgi.FieldStorage()  
name = str( form.getvalue('person_name') ) 
 
print ("Content-type:text/html\n\n") 
print ("<html>") 
print ("<head>")     
print ("</head>") 
print ("<body>") 
print ("Hello " + name ) 
print("<br/>") 
print ("all params: " + str(form)  ) 
print ("</body>") 
print ("</html>") 

但是,当我执行 HTML 文件并键入“Mike”作为名称时,我的 python 脚本会打印:

Hello None 
all params: FieldStorage(None, None, [])  

cgi.FieldStorage() 是空的。 我该如何解决?

请您参考如下方法:

这很难说。

<form action="test.py" method="get">错误?

应该是<form action="cgi-bin/test.py" method="get">

如果“cgi-bin”不是 url 的一部分,则不会执行 sgi 脚本。甚至令人惊讶的是你会得到某种回应。