python3中flask学习

pip install flask

server.py:

#!/usr/bin/env python
# coding=utf-8

import flask

app = flask.Flask(__name__)

# 默认get方法发送数据
@app.route("/")
def getPC():
    try:
        p = flask.request.args.get("province")if "province" in flask.request.args else ""
        c= flask.request.args.get("city")if "city" in flask.request.args else ""
        return p+","+c
    except Exception as e:
        print(e)

# POS方法发送数据
# @app.route("/",methods=["POST"])
# def index():
#     try:
#         print(flask.request.form)
#         p = flask.request.form.get("province")if "province" in flask.request.form else ""
#         c = flask.request.form.get("city")if "city" in flask.request.form else ""
#         # note = flask.request.form.get("note")if "note" in flask.request.form else ""
#         return p+","+c
#     except Exception as e:
#         print(e)

# GET和POS方法混合
@app.route("/",methods=["GET","POST"])
def index2():
    try:
        print(flask.request.values)
        p = flask.request.values.get("province")if "province" in flask.request.values else ""
        c = flask.request.values.get("city")if "city" in flask.request.values else ""
        note = flask.request.values.get("note")if "note" in flask.request.values else ""
        return p+","+c+"\n"+note
    except Exception as e:
        print(e)

@app.route("/hi")
def hi():
    return "<h1>欢迎光临,你好!</h1>"

if __name__ == "__main__":
    app.run()

client.py:

#!/usr/bin/env python
# coding=utf-8
# 客户端

import urllib.request
import urllib.parse

url = "http://127.0.0.1:5000"
try:
    p = urllib.parse.quote("广东")
    # p = urllib.request.quote("广东")
    c = urllib.parse.quote("深圳")
    data = "province="+p+"&city="+c
    # print(url+"?"+data)
    request = urllib.request.urlopen(url+"?"+data)
    response = request.read()
    html = response.decode()
    print(html)
except Exception as e:
    print(e)

# POS方法
try:
    province = urllib.parse.quote("河北省")
    city = urllib.parse.quote("石家庄")
    pc = "province="+province+"&city="+city
    request = urllib.request.urlopen(url,data=pc.encode())
    response = request.read()
    html = response.decode()
    print(html)
except Exception as e:
    print(e)

# GET和POS方法混合
try:
    p = urllib.parse.quote("江苏省")
    c = urllib.parse.quote("南京")
    note = "南京是江苏省省会!!!"
    note = "note=" + urllib.parse.quote(note)
    pc  = "province="+p+"&city="+c
    request = urllib.request.urlopen(url+"?"+pc,data=note.encode())
    response = request.read()
    html = response.decode()
    print(html)
except Exception as e:
    print(e)

执行效果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值