1. Code:
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"> <input type = "text" name = "yourage"> <label> In order to serve you better, please enter your age <br> </ label> <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 = " Hello, please enter your age ~ " if ' cookie ' in self.headers: try : c = cookies.SimpleCookie (self.headers [ ' cookie ' ]) age = c [ ' yourage ' ] .value if int (age)> 40 : message = " your age More than 40 years old, strengthen exercise ~, I have fitness clothes " elif int (age)> 25 : message = " Your age is less than 25 years old, do you have makeup, do I have cosmetics " else : message =" xxx, less than 25 years old, learn to improve every day " except (KeyError, cookies.CookieError) as e: message = " Sorry, I did not find your information " 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. Operation and debugging:
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$
Unsuccessful: