sanic..... 뭔가 기능이 많은듯 한데 제대로 못써먹고 있는것 같다 -ㅁ-;; 공식 레퍼런스가 넘나 광활하네요..
여튼 잘나가는 회사는 sanic을 다 쓴다고 해서 저도 써보았습니다(불순한 의도ㅋㅋㅋㅋ)
flask를 사용할때는 잘 몰라서;; param들을 하나씩 다 머리위에 이고다니느라 스트레스 많이 받은 끝에.....
init 단계에서 필요한 value들을 다 가지고 다니려고 그 부분도 써놓았어요..
from sanic import Sanic, response
from sanic.views import HTTPMethodView
from projectName import InitParams
import json, request
from projectName import service
app = Sanic(__name__)
class Main(HTTPMethodView):
def post(self, request):
try:
body = json.loads(request.body)
args = dict(id=body['id'], password=body['password'])
init = InitParams() #초기화
init.id = args['id'] #아이디와 암호 저장
init.password = args['password']
response = dict(status_code=200, msg='success') #res make
service.func(init)
return response.json(res)
except Exception as e:
res['status_code'] = 500
res['msg'] = 'fail'
finally:
return response.json(res)
app.add_route(Main.as_view(), "/post/id/<id>/password/<password>") #localhost:5000/post/id/test_id/password/testpassword
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True, access_log=True) #배포할 때 debug는 지웁시다.. 로그가 너무 많이 쌓임 ㅠㅠ
class InitParams(object):
def __init__(self, id=None, password=None):
self.id = id
self.password = password
def func(init):
print(init)
{'id':'test'} 뭐 이런식...post으로 json을 받아서 굴리는 예제입니다.
app.route가 된다고 인터넷에는 많이들 써있던데 저는 왜 안되지....
https://buildmedia.readthedocs.org/media/pdf/sanic-zh/latest/sanic-zh.pdf
from sanic import Sanic, response
from sanic.views import HTTPMethodView
from projectName import InitParams
import json, request
from projectName import service
app = Sanic(__name__)
#구글링 해보면 app.route('/post', methods=['POST']) 로도 된다는데 난 안됨 ㅠㅠ 왜일까..?
@app.post("/post")
def post_test(req):
body = json.loads(req.body)
#{'id':'test'}
return response.json(body)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True, access_log=True) #배포할 때 debug는 지웁시다.. 로그가 너무 많이 쌓임 ㅠㅠ
'Python-Framework > Python-Framework__Sanic' 카테고리의 다른 글
sanic 멸망편 (0) | 2021.02.10 |
---|---|
Sanic, gUnicorn, Nginx 삽질일기 2) (0) | 2020.03.18 |
Sanic, gUnicorn, Nginx 삽질일기 1) (0) | 2020.03.18 |
no module named sanic (0) | 2020.02.17 |