首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不支持的方法POST,通过简单python服务器

不支持的方法POST,通过简单python服务器
EN

Stack Overflow用户
提问于 2016-04-12 17:26:04
回答 1查看 9.4K关注 0票数 4

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

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

该程序正在创建一个简单的服务器,该服务器在GET请求上打印一条问候消息。http:localhost:8080/hello也给用户一个表单来输入一条新的问候信息,但是当输入时,给出501错误。POST方法应该显示与用户输入的问候语相同的页面。我用CGI来完成这件事。

我被卡住了!此外,如果有人可以提供链接/提示如何进行调试这些事情,我将不胜感激!像我能读的日志文件之类的吗?

该方案:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-24 03:56:37

您的do_POST函数没有正确缩进,它现在就在类之外,使它与do_GET函数对齐,并且应该很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36580153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档