1. 程式人生 > >Flask學習 4 http方法

Flask學習 4 http方法

requests default lang zip con useragent from index agent

服務端端

#!/usr/bin/env python  
# encoding: utf-8  
""" 
@version: v1.0 
@author: cxa 
@file: flask04.py 
@time: 2018/04/13 16:06 
"""
"""
HTTP 方法
"""
from flask import Flask,request
app=Flask(__name__)
@app.route("/")
def Index():
    return "<h1>Index Page</h1>"
@app.route(‘/register‘,methods=[‘GET‘,‘POST‘])
def register():
    print(request.headers)
    print(request.form)
    print(request.form[‘name‘])
    print(request.form.get(‘name‘))
    print(request.form.getlist(‘name‘))
    print(request.form.get(‘nickname‘,default=‘little apple‘))
    return "welcome"
if __name__==‘__main__‘:
    app.run(debug=True)

  客戶端:

#!/usr/bin/env python  
# encoding: utf-8  
""" 
@version: v1.0 
@author: cxa 
@file: client.py 
@time: 2018/04/13 16:17 
"""
import requests
from fake_useragent import UserAgent
headers = {
    ‘User-Agent‘: UserAgent().random,
    ‘Accept-Encoding‘: ‘gzip, deflate, sdch‘,
    ‘Accept-Language‘: ‘zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3‘,
    ‘Accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘,
    ‘Connection‘: ‘keep-alive‘
}

user_info={‘name‘:‘cxa‘,‘password‘:‘123‘}
r=requests.post("http://127.0.0.1:5000/register",data=user_info,headers=headers)
print(r.text)

  

Flask學習 4 http方法