0

我的 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>

我在 cgi-bin 目录中的 PYTHON 代码(test.py):

#!usr/bin/python

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

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

4

2 回答 2

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则应该这样做。

于 2013-10-24T10:05:21.613 回答
0
with open('/path/to/form.txt','w') as out_fh:
    out_fh.write(form.firstname)
    out_fh.write(form.lastname

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

于 2013-10-24T10:04:10.310 回答