수업소개
반복문과 파일목록을 가져오는 기능을 활용해서 글목록 기능을 구현하는 방법을 살펴봅니다.
강의
소스코드
index.py
#!/usr/local/bin/python3
print("Content-Type: text/html")
print()
import cgi, os
files = os.listdir('data')
listStr = ''
for item in files:
listStr = listStr + '<li><a href="index.py?id={name}">{name}</a></li>'.format(name=item)
form = cgi.FieldStorage()
if 'id' in form:
pageId = form["id"].value
description = open('data/'+pageId, 'r').read()
else:
pageId = 'Welcome'
description = 'Hello, web'
print('''<!doctype html>
<html>
<head>
<title>WEB1 - Welcome</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="index.py">WEB</a></h1>
<ol>
{listStr}
</ol>
<h2>{title}</h2>
<p>{desc}</p>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=listStr))

