應用上一篇的 Python檔案伺服器 ,我們還可以寫一些不同的回應,像是.. 1. 回應Client端要求HTML檔案 def responseHtmlFile(hdl, path): f = open(path, "rb") fs = os.fstat(f.fileno()) hdl.send_response(200) hdl.send_header("Content-type", "text/html; charset=UTF-8") hdl.send_header("Content-Length", str(fs[6])) hdl.send_header("Last-Modified", hdl.date_time_string(fs.st_mtime)) hdl.end_headers() shutil.copyfileobj(f, hdl.wfile) f.close() del f 用法: responseHtmlFile(hdl, '/home/user/your_html_file.htm') 2. 回應Client端,要求轉址到另一個目標位置 def responseHttpRedirect(hdl, target): hdl.send_response(301) hdl.send_header("Location", target) hdl.end_headers() 用法: responseHttpRedirect(hdl, 'http://www.google.com') 3. 把Client的request資訊parse後再回應給Client def responseGetRequest(hdl): parsed = urlparse(hdl.path) hdl.se...