티스토리 뷰

"""
API를 처리한다.
"""
@app.route('/api',methods=["POST"])
def api():
"""
requset 사용 요령
# query = request.form['query'] # post 방식 일때
query = request.args.get('query') # get 방식 일때
# results1 = request.args.get('results1', '0-100') # 이렇게 숫자로 제한을 둘수도 있다.
"""
 
# POST requset 데이터를 json으로 변환하는 방법
bytes = request.data
jsonDict = json.loads(bytes.decode("utf-8"))
 
# Security에 관한 것들을 처리한다.
try:
key = jsonDict['key']
except:
print("key 인자가 없습니다.")
return "요청 실패"
 
# 명령어에 대한 것을 처리한다.
try:
cmd = jsonDict['cmd']
except:
print("cmd 인자가 없습니다.")
return "요청 실패"
 
if cmd == "u01":
pass
elif cmd == "u02":
pass
elif cmd == "u90":
try:
shcode = jsonDict['shcode']
print(shcode)
except:
print("shcode 인자가 없습니다.")                
return "요청 실패"
 
try:
hname = jsonDict['hname']
print(hname)
except:
print("hname 인자가 없습니다.")
return "요청 실패"
 
try:
cursor = g.db.cursor()
cursor.execute("insert into List values('{0}','{1}')".format(shcode,hname))
g.db.commit()
except:
print("u90 : 데이터베이스 에러발생")
 
else:
return "요청 실패"
return "요청 성공"
댓글