• 利用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$ 

    未成功:

  • 相关阅读:
    nodejs理解
    jquery练习
    SASS
    css 继承和层叠
    Session cookie
    vue框架里边的ref='xxx' 的用法 在父组件里边操作子组件的变量 有多组单选按钮(遍历生成)每次只能选中一组
    获取不到页面元素的宽度??????
    刷题很重要
    复盘很重要
    jquery里边的ajax请求,如果是发get请求,就不需要写contentType:'application/json;charset=uft-8'。只有post请求才需要写这个属性。
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/12691953.html
Copyright © 2020-2023  润新知