????????HTTPЭ?飬????????????????????????????????????????????HTTPЭ?????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????URL??????????????????????????????????HTTPЭ?飬???????????????????????????????????????????????????????IP??????????????????????????request??????????IP?????????????????????????????????????????????response?????????
????????????????????????????????м???????????????GET??POST?????????
???????????????????????????????GET??POST?????web??á???????????????????????web????????????????????????web??????????????????
??????flask????????????
????GET????
pyfl/
|---- /hello.py
|----/templates/
|----|-----------/index.html
|----|-----------/user.html
hello.py
from flask import Flask??render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True)
index.html
<h1> This is index page <h1>
???????flask??????
?????????http://127.0.0.1:5000/
???????firebug??GET?????????
?????????????????????????????棬?????????κ????????????????ж????????200???ɡ?
????????hello.py???£?
from flask import Flask??render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/user/<name>")
def user(name):
return render_template("user.html"??name=name)
if __name__ == '__main__':
app.run(debug=True)
user.html
<h1> Hell?? {{name}} !<h1>
?????????http://127.0.0.1:5000/user/aaa
????????????????GET?????????Щ????????????????Щ??????aaa?????????hello.py??????????????????????????????user.html????С?
????????????????????????????Щ?????????????????????????????????sql ??????????????????sql?????????????д???sql?????????
????POST????
pyfl/
|---- /hello.py
|----/templates/
|----|-----------/index.html
hello.py
from flask import Flask??render_template??request
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/login"??methods = ['GET'?? 'POST'])
def login():
if request.method == "POST":
username = request.form.get('username')
password = request.form.get('password')
if username=="zhangsan" and password=="123":
return "<h1>welcome?? %s !</h1>" %username
else:
return "<h1>login Failure !</h1>"
else:
return "<h1>login Failure !</h1>"
if __name__ == '__main__':
app.run(debug=True)
index.html
<form action="/login" method="post">
username: <input type="text" name="username">
password: <input type="password" name="password">
<input type="submit" id="submit">
</form>
?????????http://127.0.0.1:5000/
??????????????????????????hello.py?ж?????????“zhangsan”???????“123”?????????????????????
????Python???????requests?????????????????POST????
????#coding=utf-8
????import requests
????s = requests
????data={"username":"zhangsan"??"password":"123"??}
????r = s.post('http://127.0.0.1:5000/login'?? data)
????print r.status_code
????print r.headers['content-type']
????print r.encoding
????print r.text
??????н????
????200
????text/html; charset=utf-8
????utf-8
????<h1>welcome?? zhangsan !</h1>
????POST???????????????????????????????????????????鷵????????
????===================
?????????????????????????????к??????????????json?????????????????????????????????????????????????????????????Щ???????????????в?????????????????????????