一、Python有很好的開源模塊,但是模塊能做什么事情,可以使用dir()去查看模塊下包含什么方法
例如 引入 import math 模塊,而math模塊可以做什么事情,就用dir(math)查看就可以。
用type()可以查看類型。 ?
math下面有一個pow()方法,想要知道這個函數(shù)的使用方法,可以用help(math.pow)查看。
二、Python 有很多轉(zhuǎn)義符
三、Python序列的基本操作
len() 返回序列長度
+:連接兩個序列
*:重復(fù)序列元素 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
導(dǎo)入request和make_response;
1 request對象
method:當(dāng)前請求方法(POST,GET等)
url:當(dāng)前鏈接地址
path:當(dāng)前鏈接的路徑
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:響應(yīng)狀態(tài)
headers:響應(yīng)頭,設(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)絡(luò)的Headers可以查看到
returnresponse
if__name__?=='__main__':
app.run(debug=True)