一、Python有很好的開源模塊致份,但是模塊能做什么事情变抽,可以使用dir()去查看模塊下包含什么方法
例如 引入 import math 模塊,而math模塊可以做什么事情知举,就用dir(math)查看就可以瞬沦。
用type()可以查看類型。 ?
math下面有一個pow()方法雇锡,想要知道這個函數(shù)的使用方法逛钻,可以用help(math.pow)查看。
二锰提、Python 有很多轉(zhuǎn)義符
三曙痘、Python序列的基本操作
len() 返回序列長度
+:連接兩個序列
*:重復序列元素 print("*" *20)
in:判斷元素是否存在于序列中 ? ?"a“ in str
max():返回最大值
min(): 返回最小值
cmp(x,y): 比較兩個序列值是否相同
list(reversed([1,2,3,4,5,6])) 反轉(zhuǎn)立肘,值為6边坤,5,4谅年,3茧痒,2,1
四融蹂、數(shù)組
append是將某個元素添加到數(shù)組中 例如:a = [1,2,3,4], a.append(5), a = [1,2,3,4,5]
extend是將數(shù)組添加到前面數(shù)組中 例如:a = [1,2,3,4] ,b= ['hello','world'], a.extend(b), a = [1,2,3,4,'hello','world']
五旺订、requsts和response
導入request和make_response弄企;
1 request對象
method:當前請求方法(POST,GET等)
url:當前鏈接地址
path:當前鏈接的路徑
environ:潛在的WSGI環(huán)境
headers:傳入的請求頭作為字典類對象
data:包含傳入的請求數(shù)據(jù)作為
args:請求鏈接中的參數(shù)(GET參數(shù))区拳,解析后
form:form提交中的參數(shù)拘领,解析后
values:args和forms的集合
json:json格式的body數(shù)據(jù),解析后
cookies:cookie讀取
2 response對象
2.1 生成response對象
response?=?make_response(render_template(index.html))
2.2 方法
status:響應狀態(tài)
headers:響應頭樱调,設(shè)置http字段
set_coockie:設(shè)置一個cookie
舉例:
#-*-coding:utf8-*-
fromflaskimportFlask,?flash,?request,?make_response
app?=?Flask(__name__)
app.jinja_env.line_statement_prefix?='#'
app.secret_key?='123'
@app.route('/request')
defrequestdemo():
key?=?request.args.get('key','defaultkey')
res?=?request.args.get('key','defaultkey')+'
'res+=?request.url+'
'+request.path+'
'response=make_response(res)
response.set_cookie('rainbow',?key)
response.headers['rainbow']?='twc'#?在Chrome瀏覽器的檢查中網(wǎng)絡的Headers可以查看到
returnresponse
if__name__?=='__main__':
app.run(debug=True)