今天,我們把路徑中的參數化成字典的形式:類似
/ss?message=hello world&author=fei
定義一個函數:get_path()
def get_path(path):
"""
/ss?message=hello world&author=fei
返回如下字典形式
{
'message': 'hello world',
'author': 'fei',
}
"""
# 注意find函數,如果沒有找到返回-1,如果有則返回開始索引
index = path.find('?')
if index == -1:
return path, {}
else:
path, query_string = path.split('?', 1)
args = query_string.split('&')
query = {}
for arg in args:
k, v = arg.split('=')
query[k] = v
return path, query
函數最後返回的是兩個值,路徑path與化爲字典形式的參數。