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>

  

 

转载于:https://www.cnblogs.com/byczyz/p/11116879.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值