from flask import Flask
app = Flask(__name__)
topics = [
{"id":1, "title":"html", "body":"html is ...."},
{"id":2, "title":"css", "body":"css is ...."},
{"id":3, "title":"js", "body":"js is ...."}
]
def template(content):
liTags = ''
for topic in topics:
liTags = liTags + f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
return f'''
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{liTags}
</ol>
{content}
<ul>
<li><a href="/create/">create</a></li>
</ul>
</body>
</html>
'''
@app.route("/")
def index():
return template('<h2>Welcome</h2>Hello, WEB!')
@app.route("/read/<int:id>/")
def read(id):
title = ''
body = ''
for topic in topics :
if topic['id'] == id:
title = topic['title']
body = topic['body']
break;
return template(f'<h2>{title}</h2>{body}')
@app.route('/create/')
def create():
content = '''
<form action="/create/">
<p><input type="text" name="title" placeholder="title"></p>
<p><textarea name="body" placeholder="body"></textarea></p>
<p><input type="submit" value="create"></p>
</form>
'''
return template(content)
@app.route('/update/')
def update():
return 'Update'
app.run()
서버에 관한 내용이 어려웠습니다.
관련 개념을 찾아보았습니다!
크게 아래와 같은 순서로 요청을 처리한다.
1. http/https Request가 들어오면
2. 웹 서버가 해당 Request를 받고
3. WSGI 미들에워를 통해 파이썬 어플리케이션으로 Request 전달
4. 파이선 어플리케이션이 Request를 받아 처리 후, WSGI 미들웨어 - 웹서버를 통해 Response 리턴
여러 사용자들이 동시에 사이트에 접근할 경우, 이것을 처리하는 역할을 웹서버가 합니다.
웹서버에 관해 찾아보다가 apache가 프로세스를 포크하는 방식이라는 내용이 와닿지 않아서 관련 내용을 학습해야 될 것 같습니다..!