python3中flask学习
pip install flaskserver.py:#!/usr/bin/env python# coding=utf-8import flaskapp = flask.Flask(__name__)# 默认get方法发送数据@app.route("/")def getPC():try:p = flask.request.args.get...
·
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)