zoukankan      html  css  js  c++  java
  • flask get,post访问方式

    from flask import *
    
    '''
    服务器用flask中的request对象的args来存储GET的参数,用get方法
    来获取参数,即用flask.request.args.get(参数)来获取参数的值
    '''
    app=Flask(__name__)
    @app.route('/')
    def index():
        try:
            name=request.args.get("name") if "name" in request.args.get else ""
            age=request.args.get("age") if "age" in request.args.get else ""
            return name+","+age
        except Exception as err:
            print(err)
    
    if __name__=='__main__':
        app.run(port=5000,debug=True)
    

      

    import  urllib.parse
    import  urllib.request
    url="http://127.0.0.1:5000"
    try:
        #如果传参有汉字需要使用urllib.prase.quote()
        name=urllib.parse.quote("XXXX")
        age=urllib.parse.quote("二十")
        data="name="+name+"&age="+age
        html=urllib.request.urlopen("http://127.0.0.1:5000?"+data)
        html=html.read()
        html=html.decode()
        print(html)
    except Exception as err:
        print(err)
    

      

    import urllib.request
    import urllib.parse
    
    url="http://127.0.0.1:5000"
    name="XXXXXXX"
    age="21"
    note="post传值实验。这是我的post传值实验"
    
    name=urllib.parse.quote(name)
    age=urllib.parse.quote(age)
    note=urllib.parse.quote(note)
    
    data="name="+name+"&age="+age+"&note="+note
    
    resp=urllib.request.urlopen(url,data=data.encode())
    data=resp.read()
    html=data.decode()
    
    print(html)
    

      

    from flask import *
    #服务器端
    app=Flask(__name__)
    @app.route("/",methods=["GET","POST"])
    def index():
         try:
              name=request.form.get("name") if "name" in request.form.get else ""
              age=request.form.get("age") if "age" in request.form.get else ""
              note=request.form.get("note") if "note" in request.form.get else ""
              print(name)
              print(age)
              print(note)
              data=name+"\n"+age+"\n"+note
              return data
         except Exception as err:
              print(err)
    
    
    if __name__=='__main__':
        app.run(port=5000,debug=True)
    

      

    一个小型的爬虫

    import urllib.request
    import urllib.parse
    '''
    小型爬虫地址
    '''
    url="http://127.0.0.1:5000"
    html=urllib.request.urlopen(url)
    html=html.read()
    html=html.decode()
    print(html)
    

      

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>实验</title>
    </head>
    <body>
    <h1>欢迎使用python web flask 框架</h1>
    <p>
        这是我的一个爬虫以及flask框架的测试
    </p>
    </body>
    </html>
    

      

    一纸高中万里风,寒窗读破华堂空。 莫道长安花看尽,由来枝叶几相同?
  • 相关阅读:
    用超穷归纳证明极大理想定理
    [shell]计算未知个数数据的平均值
    C中的野指针—如何避免
    [C#学习笔记02]理解值和引用
    vim编辑器的配置让你的代码更加易读
    mipslinuxgcc交叉编译工具链搭建小结
    Ubuntu 9.10下KScope的安装
    [uC/OSII]基本操作:新建任务,分配信号量
    基于Philips MF RC522的ISO14443A标准卡读写
    Git常用命令 shuo
  • 原文地址:https://www.cnblogs.com/byczyz/p/11116879.html
Copyright © 2011-2022 走看看