zoukankan      html  css  js  c++  java
  • 利用Cookie精准营销==

    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$ 

    未成功:

  • 相关阅读:
    “指定的SAS安装数据(sid)文件不能用于选定的SAS软件订单
    windows下如何快速优雅的使用python的科学计算库?
    量化分析师的Python日记【第1天:谁来给我讲讲Python?】
    Python的lambda函数与排序
    使用python管理Cisco设备-乾颐堂
    python移除系统多余大文件-乾颐堂
    python算法
    python实现高效率的排列组合算法-乾颐堂
    使用python把图片存入数据库-乾颐堂
    Python将阿拉伯数字转化为中文大写-乾颐堂
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/12691953.html
Copyright © 2011-2022 走看看