def do_POST(self): parsed_path = urlparse(self.path) parsed_qs = parse_qs(parsed_path.query) if parsed_path.path == '/cow': try: buttcow = cow.Stegosaurus() msg = buttcow.milk(parsed_qs['msg'][0]) msg_json = json.dumps({'content': msg}) self.send_response(200) self.send_header('Content-Type', 'application/json') self.end_headers() print(msg_json) self.wfile.write(msg_json.encode()) return except KeyError: self.send400() else: self.send_response(404) self.end_headers() buttcow = cow.Ghostbusters() msg = buttcow.milk('404 ERROR!!! NOT FOUND!!!!') self.wfile.write(msg.encode())
def do_GET(self): parsed_path = urlparse(self.path) parsed_qs = parse_qs(parsed_path.query) # set a status code # set any headers # set any body data on the response # end headers if parsed_path.path == '/': self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write(b'<html><body><h1>Hello world</h1></body></html>') return elif parsed_path.path == '/cow': try: buttcow = cow.Stegosaurus() msg = buttcow.milk(parsed_qs['msg'][0]) self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write(msg.encode()) return except KeyError: self.send400() else: self.send_response(404) self.end_headers() buttcow = cow.Ghostbusters() msg = buttcow.milk('404 ERROR!!! NOT FOUND!!!!') self.wfile.write(msg.encode())
def do_GET(self): parsed_path = urlparse(self.path) parsed_qs = parse_qs(parsed_path.query) # set a status code # set any headers # set any body data on the response # end headers if parsed_path.path == '/': self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() html = dedent(''' <html> <head> <title> cowsay </title> </head> <body> <header> <nav> <ul> <li><a href="/cow">cowsay</a></li> </ul> </nav> <header> <main> </main> </body> </html>''') self.wfile.write(html.encode()) return elif parsed_path.path == '/cow': try: buttcow = cow.Stegosaurus() msg = buttcow.milk(parsed_qs['msg'][0]) self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write(msg.encode()) return except KeyError: self.send400() else: self.send_response(404) self.end_headers() buttcow = cow.Ghostbusters() msg = buttcow.milk('404 ERROR!!! NOT FOUND!!!!') self.wfile.write(msg.encode())
def do_GET(self): """ gets the parsed url request""" parsed_path = urlparse(self.path) parsed_qs = parse_qs(parsed_path.query) # set a status code # set any headers # set any body data on the response # end headers if parsed_path.path == '/': self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write( b'<html><body><h1>Hello World!</h1></body></html>') return elif parsed_path.path == '/cow': try: cow_image = cow.Stegosaurus() msg = cow_image.milk(parsed_qs['msg'][0]) self.send_response(200) # self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write(msg.encode()) return except KeyError: different_cow = cow.BongCow() msg = different_cow.milk("400") self.send_response(400) self.send_header('Content-Type', 'text/plain') self.end_headers() self.wfile.write(msg.encode()) else: cow_ghost = cow.Ghostbusters() msg = cow_ghost.milk("404") self.send_response(404) self.end_headers() self.wfile.write(msg.encode())