1.代码:
from http.server import HTTPServer,BaseHTTPRequestHandler from http import cookies from urllib.parse import parse_qs from html import escape as html_escape form = '''<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>小心,网站正在读取你的信息</title> <p> {} <p> <form method="POST"> <label>为了哥哥好的为您服务,请输入年龄 <input type="text" name="yourage"> </label> <br> <button type="submit">傻呼呼的提交年龄~</button> </form> </head> <body> </body> </html> ''' class ageHandler(BaseHTTPRequestHandler): def do_POST(self): length = self.headers.get('Content-length',0) data = self.read(length).decode() yourage = parse_qs(data)["yourage"][0] c = cookies.SimpleCookie() c["yourage"] = yourage c["yourage"]["domain"] = 'localhost' c["yourage"]["max-age"] =60 self.send_response[303] self.send_header('location','/') self.send_header('Set-Cooke',c['yourage'].OutputString()) self.end_headers() def do_GET(self): message="您好,请您输入您的年龄~" if 'cookie' in self.headers: try: c=cookies.SimpleCookie(self.headers['cookie']) age = c['yourage'].value if int(age)>40: message="您的年龄大于40岁,加强锻炼吧~,我有健身服装" elif int(age)>25: message="您的年龄小于25岁,化妆吗,我有化妆品" else: message="xxx,小于25岁,好好学习天天向上" except (KeyError,cookies.CookieError) as e: message="不好意思,我没有找到您的信息" print(e) self.send_response(200) self.send_header('Content-type','text/html;charset=utf-8') self.end_headers() msg = form.format(message) self.wfile.write(msg.encode()) if __name__ == "__main__": server_address =('',9994) httpd = HTTPServer(server_address,ageHandler) httpd.server_activate()
2.运行及调试:
Last login: Thu Apr 9 16:59:33 on ttys003 (base) localhost:~ ligaijiang$ cd /Users/ligaijiang/FullStackDeveloper/html (base) localhost:html ligaijiang$ ls AdServer.py style.css This is my first HTML.html tieba.html jmeter.log tieba.py python_server.py white hat.jpg (base) localhost:html ligaijiang$ python3 AdServer.py File "AdServer.py", line 18 </label> <button type="submit">傻呼呼的提交年龄~</button> </form> ''' ^ SyntaxError: invalid syntax (base) localhost:html ligaijiang$ python3 AdServer.py File "AdServer.py", line 32 self.send_header)('location','/') ^ SyntaxError: invalid syntax (base) localhost:html ligaijiang$ python3 AdServer.py File "AdServer.py", line 42 age = c['yourage'].value~ ^ SyntaxError: invalid character in identifier (base) localhost:html ligaijiang$ python3 AdServer.py File "AdServer.py", line 49 except: (KeyError.cookies.ConnectError) as e: ^ SyntaxError: invalid syntax (base) localhost:html ligaijiang$ python3 AdServer.py File "AdServer.py", line 49 except: (KeyError,cookies.CookieError) as e: ^ SyntaxError: invalid syntax (base) localhost:html ligaijiang$ python3 AdServer.py File "AdServer.py", line 49 except: (KeyError,cookies.CookieError) as e: ^ SyntaxError: invalid syntax (base) localhost:html ligaijiang$ python3 AdServer.py File "AdServer.py", line 63 httpd.server_activate)() ^ SyntaxError: invalid syntax (base) localhost:html ligaijiang$ python3 AdServer.py (base) localhost:html ligaijiang$ (base) localhost:html ligaijiang$ python3 AdServer.py (base) localhost:html ligaijiang$ (base) localhost:html ligaijiang$ python3 AdServer.py (base) localhost:html ligaijiang$ (base) localhost:html ligaijiang$ python3 AdServer.py (base) localhost:html ligaijiang$ (base) localhost:html ligaijiang$ (base) localhost:html ligaijiang$ python3 AdServer.py (base) localhost:html ligaijiang$
未成功: