728x90
반응형
[hello01]
# -*- coding: utf-8 -*-
from flask import Flask
app = Flask(__name__)
@app.route('/') #localhost:5000으로 요청 해서 헬로월드로 리턴
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
# 결과값
* Serving Flask app "hello01" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
# 여기서 나온 링크로 들어가본다!
[hello03]
# -*- coding: utf-8 -*-
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST']) #get,post 모두 받을 수 있도록 해준다.
def hello_root():
html = '''
<h1>GET/POST<h1>
<a href="/test">get</a>
<form action="/test" method="POST">
<input type="submit" value="post"/>
</form>
'''
return html
@app.route('/test', methods=['GET', 'POST'])
def hello_test():
if request.method == 'POST':
return '<h1 style="color:blue;">Request Post</h1>'
else:
return '<h1 style="color:red;">Request Get</h1>'
if __name__ == '__main__':
app.run()
# 결과값
* Serving Flask app "hello03" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[get 클릭 화면]
[post 클릭 화면]
[hello04]
# -*- coding: utf-8 -*-
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_root():
return '<h1><a href="/test/heesun">go test</a></h1>'
#마지막/뒤에 단어에 따라 아래 메소드 안의 hello 뒤의 단어가 바뀐다.
@app.route('/test/<id>')
def hello_test(id):
return '<h1>Hello, ' + id + '</h1>'
if __name__ == '__main__':
app.run()
728x90
반응형
'Language > Python' 카테고리의 다른 글
[코딩도장]03~05.기본문법 (0) | 2022.03.12 |
---|---|
[python]flask02_render_template (0) | 2020.10.13 |
[python]crawling_크롤링_starbucks (0) | 2020.10.13 |
[python]crawling_크롤링_인스타 (0) | 2020.10.12 |
[python]crawling_크롤링_naver02 (0) | 2020.10.12 |