简单python服务器不支持的方法POST

简单python服务器不支持的方法POST,python,http,server,Python,Http,Server,我正在学习Udacity的全栈开发,我们必须使用http.server创建一个简单的python服务器 我用python编写了一个小程序,可以很好地处理GET请求。 我对这篇文章有意见。 服务器正在端口8080(本地主机)上运行。我给出的任何POST请求都返回501不支持的方法错误。 我主要研究内核设备驱动程序等,我不习惯调试这样的错误 该程序正在创建一个简单的服务器,在GET请求时打印问候消息。 http:localhost:8080/hello 它还为用户提供了一个表单来输入新的问候语,但是

我正在学习Udacity的全栈开发,我们必须使用http.server创建一个简单的python服务器

我用python编写了一个小程序,可以很好地处理GET请求。 我对这篇文章有意见。 服务器正在端口8080(本地主机)上运行。我给出的任何POST请求都返回501不支持的方法错误。 我主要研究内核设备驱动程序等,我不习惯调试这样的错误

该程序正在创建一个简单的服务器,在GET请求时打印问候消息。
http:localhost:8080/hello
它还为用户提供了一个表单来输入新的问候语,但是当输入时,会出现501错误。POST方法应该显示与用户输入的问候语相同的页面。我正在使用CGI来完成这项工作

我卡住了! 另外,如果有人能提供如何调试这些东西的链接/提示,我将不胜感激!有没有我可以读的日志文件之类的

该方案:

from http.server import BaseHTTPRequestHandler, HTTPServer
import cgi


class WebServerHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        if self.path.endswith("/hello"):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            output = ""
            output += "<html><body>"
            output += "<h2> How's it going?</h2>"
            output += "<form method = 'POST' enctype='multipart/form-data' action='/hello'> What would you like me to say?</h2><input name = 'message' type = 'text'> <input type = 'submit' value = 'Submit'></form>"
            output += "</body></html>"
            self.wfile.write(output.encode(encoding='utf_8'))
            print (output)
            return
        if self.path.endswith("/hola"):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            output = ""
            output += "<html><body>&#161hola  <a href = /hello> Back to English! </a>"
            output += "<form method = 'POST' enctype='multipart/form-data' action='/hello'> What would you like me to say?</h2><input name = 'message' type = 'text'> <input type = 'submit' value = 'Submit'></form>"
            output += "</body></html>"
            self.wfile.write(output.encode(encoding='utf_8'))
            print (output)
            return

        else:
            self.send_error(404, 'File Not Found:')


def do_POST(self):
        try:
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            ctype, pdict = cgi.parse_header(
                self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('message')
            output = ""
            output += "<html><body>"
            output += " <h2> Okay, how about this: </h2>"
            output += "<h1> %s </h1>" % messagecontent[0]
            output += '''<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
            output += "</body></html>"
            self.wfile.write(output.encode(encoding = "utf_8"))
            print (output)
        except:
            pass




def main():
    try:
        port = 8080
        server = HTTPServer(('', port), WebServerHandler)
        print ("Web Server running on port: 8080")
        server.serve_forever()
    except KeyboardInterrupt:
        print (" ^C entered, stopping web server....")
        server.socket.close()

if __name__ == '__main__':
    main()
从http.server导入BaseHTTPRequestHandler,HTTPServer
导入cgi
类WebServerHandler(BaseHTTPRequestHandler):
def do_获得(自我):
如果self.path.endswith(“/hello”):
自我发送_响应(200)
self.send_标题('Content-type','text/html')
self.end_头()
output=“”
输出+=“”
输出+=“进展如何?”
输出+=“您想让我说什么?”
输出+=“”
self.wfile.write(output.encode(encoding='utf_8'))
打印(输出)
返回
如果self.path.endswith(“/hola”):
自我发送_响应(200)
self.send_标题('Content-type','text/html')
self.end_头()
output=“”
输出+=“¡ HOLA”
输出+=“您想让我说什么?”
输出+=“”
self.wfile.write(output.encode(encoding='utf_8'))
打印(输出)
返回
其他:
self.send_错误(404,“找不到文件:”)
def do_POST(自我):
尝试:
自我发送_响应(200)
self.send_标题('Content-type','text/html')
self.end_头()
ctype,pdict=cgi.parse_头(
self.headers.getheader('content-type'))
如果ctype==“多部分/表单数据”:
fields=cgi.parse_multipart(self.rfile,pdict)
messagecontent=fields.get('message')
output=“”
输出+=“”
输出+=“好的,这个怎么样:”
输出+=%s”%messagecontent[0]
输出+=''你想让我说什么?''
输出+=“”
self.wfile.write(output.encode(encoding=“utf_8”))
打印(输出)
除:
通过
def main():
尝试:
端口=8080
服务器=HTTPServer(“”,端口),WebServerHandler)
打印(“在端口8080上运行的Web服务器”)
服务器。永远为您服务()
除键盘中断外:
打印(“^C已输入,正在停止web服务器…”
server.socket.close()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

您的
do\u POST
函数没有正确缩进,它现在在类之外,请将它与
do\u GET
函数对齐,应该可以。正如您在该链接中看到的,您的类不支持do\u POST。我相信这可能是错误的。尝试发布到非CGI URL时会输出错误501“只能发布到CGI脚本”,谢谢!我一直在试图修复它,但我不知道从哪里开始。任何帮助都将不胜感激:我对那个服务器不太了解,我曾经使用cherrypy,它很好用,如果你想设置一个小服务器,烧瓶或瓶子都很好用。这些是python的web框架