WEB2-Python-18.활용-반복문을 이용해 글목록 구현

홍태경·2021년 3월 3일
0

CGI_with python

목록 보기
1/1

참조 유튜브 : https://www.youtube.com/watch?v=v7UtxoqJ2bk&list=PLuHgQVnccGMDMxfZEpLbzHPZUEwObEaZq&index=30

목적 : 내 디렉토리 상태를 웹페이지도 자동으로 받아올 수 있게 하고 싶다. for문 과 os 모듈을 이용해여

차이점 : 나의 디렉토리에 python 이라는 파일을 삭제하거나 추가해도 실제 웹페이지에는 a태그를 하지 않는이상 변동되지 않는다 모든 웹페이지를 컨트롤 및 나의 디렉토리로 관리 할 수 있게 됌.

#!python
print("Content-Type: text/html; charset= euc-kr\n")
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()
    files
else:
    pageId = 'Welcome'
    description = 'Hello, web'

print(''' <!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>WEB1 - Welcome</title>
</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))
profile
나의 에고를 인정하고 사랑하자

0개의 댓글