1

我正在创建一个简单的服务器来通过 Python 提供表单。用户将通过 HTML 表单发布他们的姓名和消息,在服务器端 python 将检索这些值并将它们附加到文件中。除了 cgi.fieldstorage 之外,我已经单独测试了每个部分。运行代码时,它不会返回错误,但不会显示我的 HTML 页面。有什么建议么?

Python文件如下

#!/usr/bin/python

import sys
import os
import SimpleHTTPServer
import cgi
import SocketServer


class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        form = cgi.FieldStorage()
        user = form.getvalue("name")
        message = form.getvalue("line") 

        #Append to data.txt
        data = open('data.txt', 'a')    #open this txt file to then append to it
        data.write('%s: %s\n'% (user, message)) #Append this line to the file
        data.close()

if __name__=="__main__":    
    PORT = 9020                         #declare which port to serve on.
    server = SocketServer.TCPServer(('',PORT),MyHandler) #call the class to   generate the server
    print "Serving on port: ", PORT     #Print what port the Server is serving on
    server.serve_forever()              #Loop serving requests
4

0 回答 0