Python - BaseHTTPServer,POST和GET python3.6问题

时间:2018-06-07 12:14:03

标签: python post sqlalchemy basehttpserver

我对使用sqlalchemy的POST有疑问。这是细节 我正在尝试创建一个新餐厅",将其保存在数据库中并在餐馆页面上显示。但是,我点击了创建按钮总是失败。(例外情况称为打印(错误)被调用。)谁能告诉我我的代码有什么问题?始终显示ERR_EMPTY_RESPONSE。代码如下。

from http.server import BaseHTTPRequestHandler, HTTPServer
from http import HTTPStatus
import cgi

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base,Restaurant,MenuItem

engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()


class WebServerHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        if self.path.endswith("/restaurants"):

            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()

            restaurants = session.query(Restaurant).all()

            output = b"<a href = '/restaurants/new' > Make a New Restaurant Here </a></br></br>"
            output += b"<html><body>"
            for restaurant in restaurants:
                output += restaurant.name.encode()
                output += b"</br>"
                output += b"<html><body><a href='/edit'>Edit</a></body></html>"
                output += b"</br>"
                output += b"<html><body><a href='/delete'>Delete</a></body></html>"
                output += b"</br>"b"</br>"b"</br>"
            output += b"</body></html>"
            self.wfile.write(output)

            return
        if self.path.endswith("/restaurants/new"):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()

            output =  b"<html><body>"
            output += b"<h1>Make a New Restaurant</h1>"
            output += b"<form method = 'POST' enctype='multipart/form-data' action = '/restaurants/new'>"
            output += b"<input name = 'newRestaurantName' type = 'text' placeholder = 'New Restaurant Name' > "
            output += b"<input type='submit' value='Create'>"
            output += b"</form></html></body>"
            self.wfile.write(output)
            return
        else:
            self.send_error(404, 'File Not Found: %s' % self.path)

    def do_POST(self):
        try:

            if self.path.endwith("/restaurants/new"):

                ctype, pdict = cgi.parse_header(self.headers.get('Content-type'))
                pdict['boundary'] = bytes(pdict['boundary'], "utf-8")

                if ctype == 'mulitpart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    print("Fields value is", fields)
                    messagecontent = fields.get('newRestaurantName')
                    print("Restaurant name is ", messagecontent[0].decode("utf-8"))


                    newRestaurant = Restaurant(name=messagecontent[0].decode("utf-8"))
                    session.add(newRestaurant)
                    session.commit()

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()

        except:
            print("error")

0 个答案:

没有答案