Python3建立簡單的http server

因為需要測試Web API,所以利用python 3來建立一個簡單的http server,並傳回字串,程式如下,配合Docker還真的蠻方便的。
from http.server import HTTPServer, BaseHTTPRequestHandler

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        if (self.path == "/"):
            self.send_response(200)
            self.send_header('Content-type','text/html')
            self.send_header('Access-Control-Allow-Origin','*')
            self.end_headers()
            self.wfile.write("我是首頁".encode()) #轉成二進位
            
        if (self.path == "/get_info"):           
            self.send_response(200)
            self.send_header('Content-type','application/json')
            self.send_header('Access-Control-Allow-Origin','*')
            self.end_headers()
            self.wfile.write("資訊頁測試.encode()) #轉成二進位
           
httpd = HTTPServer(('0.0.0.0', 80), SimpleHTTPRequestHandler)
httpd.serve_forever()