我试图添加一个评论形式到我的HTML测试网站,但我不能得到的形式写评论到一个文件。你知道吗
<form action="/Users/kyle/server/comments.html" method="POST">
Your name: <br>
<input type="text" name="realname"><br>
<br>Your email: <br>
<input type="text" name="email"><br>
<br>Your comments: <br>
<textarea name="comments" rows="15" cols="50"></textarea><br><br>
<input type="submit" value="Submit">
</form>
我怎样才能得到表格来给文件写评论呢?你知道吗
这是我用于服务器的python代码
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
PORT_NUMBER = 8080
#This class will handles any incoming request from
#the browser
a = open("/Users/kyle/server/web-test.html")
site=a.read()
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message
self.wfile.write(site)
return
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
使用这段代码,您需要扩展
myHandler
来处理POST请求,然后在接受POST请求的方法中,您需要自己解析表单数据。这个站点提供了一个获取POST数据的简单示例:http://pymotw.com/2/BaseHTTPServer/#http-post。一旦从表单数据中获得了注释,就可以像在任何其他Python应用程序中一样将其写入文件。{如果你需要在这里阅读Python文档。你知道吗也就是说,像这样直接对原始请求进行操作的代码通常不是为生产使用而编写的。一般来说,web应用程序是使用一个框架开发的,这个框架中有很多部分可以为您完成这些工作。该框架通常在独立的web服务器下运行。例如,Django是一个web应用程序框架,您可以使用Apache和mod_python运行Django应用程序。你知道吗
就其他框架而言,我个人喜欢flask。您可能会发现CherryPy很有趣,因为CherryPy同时提供了一个web应用程序框架和一个运行它的web服务器,在您刚刚开始了解web应用程序时,这可能会更好地减少服务器设置问题。(flask确实附带了一个开发服务器,您可以仅用于测试,但该开发服务器很难准备好用于生产。)
相关问题 更多 >
编程相关推荐