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>

  

 

posted @ 2019-07-01 21:37  薄眠抛却陈年事。  阅读(1715)  评论(0编辑  收藏  举报