@@ -10,7 +10,7 @@ def indexGET(request):
1010# request.body: {}
1111# request.content_type: {}
1212# request.method: {}
13- # request.META. HTTP_HOST: {}
13+ # request.META[' HTTP_HOST'] : {}
1414# """.format(
1515# request.path,
1616# request.body,
@@ -29,31 +29,32 @@ def indexGET(request):
2929 # # write header
3030 # rtnRes.__setitem__('memo', 'Go home kid')
3131
32- # #-------------------------------------
33- # # deal with GET
34-
35- # if request.method == 'GET':
36- # # 刪掉網址中的 /api/
37- # preRe= re.compile('^/api/')
38- # apiName = preRe.sub('', request.path)
39-
40- # parameterGET = dict(request.GET)
41- # # http://localhost:8000/api/index?aaa=b;hhh=t
42- # # {'aaa': ['b'], 'hhh': ['t']}
43- # for eachP in parameterGET:
44- # if len(parameterGET[eachP])==1:
45- # parameterGET[eachP] = parameterGET[eachP][0]
32+ #-------------------------------------
33+ # deal with GET
34+ if request .method == 'GET' :
35+ # 刪掉網址中的 /api/
36+ preRe = re .compile ('^/api/' )
37+ apiName = preRe .sub ('' , request .path )
38+ #playData
39+ parameterGET = dict (request .GET )
40+ # http://localhost:8000/api/index?aaa=b;hhh=t
41+ # {'aaa': ['b'], 'hhh': ['t']}
42+ for eachP in parameterGET :
43+ if len (parameterGET [eachP ])== 1 :
44+ parameterGET [eachP ] = parameterGET [eachP ][0 ]
4645
47- # data = ''
48- # try:
49- # data = getattr(apiList, apiName)(parameterGET)
50- # if data['status']:
51- # data = json.dumps(data['data'])
52- # else:
53- # data = data['err']
54- # except:
55- # data = '沒有這支 api'
56- # # http://localhost:8000/api/playData?name=Charmander
46+ data = ''
47+ try :
48+ data = getattr (apiList , apiName )(parameterGET )
49+ if data ['status' ]:
50+ data = json .dumps (data ['data' ])
51+ else :
52+ data = data ['err' ]
53+ except :
54+ data = '沒有這支 api'
55+ # http://localhost:8000/api/playData?name=Charmander
56+ else :
57+ data = '你只能用GET'
5758 rtnRes .write (data )
5859 return rtnRes
5960
@@ -64,13 +65,11 @@ def indexPOST(request):
6465 if request .method == 'POST' :
6566 if request .content_type == 'application/json' :
6667 parameterPOST = json .loads ( str (request .body , encoding = 'utf8' ) )
67- apiName = parameterPOST ['api' ]
6868 try :
6969 apiName = parameterPOST ['api' ]
7070 data = getattr (apiList , apiName )(parameterPOST )
7171 except :
7272 data = "api 為必要欄位"
73-
7473 else :
7574 data = "如用 GET 請加上 api 名稱"
7675
0 commit comments