python - Web7
python flask7
1. 데이터 전달 방식
패킷이란 전달되는 데이터를 의미한다. (자세한 내용: http://www.ktword.co.kr/abbr_view.php?m_temp1=421)
Method 방식은 GET과 POST로 2가지가 있다.
GET방식은 data를 패킷의 header를 통해 가져간다.
POST방식은 data를 패킷의 body를 통해 가져간다.
데이터를 추출하기 위해서는 request 객체를 사용한다. (import 사용)
2. GET방식
GET방식의 대표적인 예는 다음과 같다.
https://search.naver.com/search.naver?sm=top_hty&fbm=1&ie=utf8&query=apple
구조: 주소 ? 데이터
데이터는 key=value&key=value.... 의 구조를 가진다.
3. GET 방식으로 data 얻기
구조: request.args.get(key)
@app.route('/test')
def test():
userName = request.args.get('name')
print(userName)
return userName
:request를 통해 주소로 들어온 value를 얻을 수 있다. args는 문자열, get은 이를 획득하라는 의미로 userName 에는 name(key)의 value가 들어간다.
4. GET방식으로 여러개 data 얻기
@app.route('/login')
def login():
userID = request.args.get('uid')
userPW = request.args.get('upw')
return 'id: %s and pw: %s' %(userID, userPW)
: uid와 upw의 value를 모두 얻어 화면에 보인다. 주소상 uid와 upw의 구분은 &을 통해 한다.
전체코드
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def home():
return 'hello world3'
@app.route('/test')
def test():
userName = request.args.get('name')
print(userName)
return 'test'
@app.route('/login')
def login():
userID = request.args.get('uid')
userPW = request.args.get('upw')
return 'id: %s and pw: %s' %(userID, userPW)
if __name__ == '__main__':
app.run(debug=True)
실행화면
1.url: http://127.0.0.1:5000/test?name=A
2. url: http://127.0.0.1:5000/test?uid=n&upw=1
댓글
댓글 쓰기