如何将html表单数据存储到文件中

时间:2013-10-24 10:00:55

标签: python

我的HTML代码:

<html>
<head>
<title>INFORMATION</title>
</head>
  <body>
    <form action = "/cgi-bin/test.py" method = "post">
        FirstName:
        <input type = "text" name = "firstname" /><br>
        LastName:
        <input type = "text" name = "lastname" /><br>

        <input type = "submit" name = "submit "value = "SUBMIT">
        <input type = "reset" name = "reset" value = "RESET">
        </form>
   </body>

我的PYTHON CODE(test.py)位于cgi-bin目录中:

#!usr/bin/python

form = web.input()
print form.firstname
print form.lastname

我应该怎么做才能将HTML数据存储在某个文件中?

2 个答案:

答案 0 :(得分:1)

只需将其写入文件即可!

#!usr/bin/python

import cgi
form = cgi.FieldStorage()
with open ('fileToWrite.txt','w') as fileOutput:
    fileOutput.write(form.getValue('firstname'))
    fileOutput.write(form.getValue'(lastname'))

哦,你需要拥有该文件的写入权限。因此,例如,如果您正在运行apache,sudo chown www-data:www-data fileToWrite.txt应该这样做。

答案 1 :(得分:0)

with open('/path/to/form.txt','w') as out_fh:
    out_fh.write(form.firstname)
    out_fh.write(form.lastname

网络服务器需要具有您要在其中创建文件的目录的写入权限。