티스토리 뷰

index.py

#!python
print("Content-Type: text/html")
print()

# 필요한 모듈들을 임포트
import html_sanitizer
import view
import os
import cgi

# 2개의 인스턴수 변수 생성
sanitizer = html_sanitizer.Sanitizer()
form = cgi.FieldStorage()

if 'id' in form: # form에 있이서 'id'가 있는지 검사
    title = pageId = form["id"].value
    description = open('data/' + pageId, 'r').read()
    # description = description.replace('<', '&lt;')
    # description = description.replace('>', '&gt;')
    description = sanitizer.sanitize(description)
    title = sanitizer.sanitize(title)
    update_link = '<a href="update.py?id={}">update</a>'.format(pageId)
    delete_action = '''
        <form action="process_delete.py" method="POST">
            <input type="hidden" name="pageId" value={}>
            <input type="submit" value="delete">
        </form>
    '''.format(pageId)
else:
    title = pageId = 'Welcome'
    description = 'Hello Web'
    update_link = ''
    delete_action = ''
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>
  <a href="create.py">create</a>
  {update_link}
  {delete_action}
  <h2>{title}</h2>
  <p>{desc}</p>
</body>
</html>
'''.format(
    title=title,
    desc=description,
    listStr=view.getList(),
    update_link=update_link,
    delete_action=delete_action))

 

create.py

#!python
print("Content-Type: text/html")
print()

import view
import os
import cgi


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>
  <a href="create.py">create</a>
  <form action="process_create.py" method="POST">  #
    <p><input type="text" name="title" placeholder="title"></p>
    <p><textarea rows="4" name="desc" placeholder="description"></textarea></p>
    <p><input type="submit"></p>
  </form>
  <h2>{title}</h2>
  <p>{desc}</p>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=view.getList()))

 

update.py

#!python
print("Content-Type: text/html")
print()

import view
import os
import cgi


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>
  <a href="create.py">create</a>
  <form action="process_update.py" method="POST">
    <input type="hidden" name="pageId" value="{form_default_title}">
    <p><input type="text" name="title" placeholder="title" value="{form_default_title}""></p>
    <p><textarea rows="4" name="desc" placeholder="description">{form_default_description}</textarea></p>
    <p><input type="submit"></p>
  </form>
  <h2>{title}</h2>
  <p>{desc}</p>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=view.getList(), form_default_title=pageId, form_default_description=description))

 

process_create.py

#!python

import cgi
form = cgi.FieldStorage()
title = form["title"].value # form에서 title이라는 이름을 가진 값을 가져옴
description = form['desc'].value # form에서 desc라는 이름을 가진 값을 가져옴

opened_file = open('data/' + title, 'w') # 해당 주소에 값을 작성하기 위한 코드
opened_file.write(description) # description 이라는 변수를 해당 주소에 작성
opened_file.close() # 오픈한 파일을 닫음

# Redirection
print("Location: index.py?id=" + title)
print()

 

process.update.py

#!python

import cgi
import os


form = cgi.FieldStorage()
pageId = form["pageId"].value
title = form["title"].value
description = form['desc'].value

opened_file = open('data/' + pageId, 'w')
opened_file.write(description)
opened_file.close()

os.rename('data/' + pageId, 'data/' + title)

# Redirection
print("Location: index.py?id=" + title)
print()

 

process_delete.py

#!python

import cgi
import os

form = cgi.FieldStorage()
pageId = form["pageId"].value

os.remove('data/' + pageId)

# Redirection
print("Location: index.py")
print()

 

view.py

import os
import html_sanitizer


def getList():
    sanitizer = html_sanitizer.Sanitizer()
    files = os.listdir('data')
    listStr = ''
    for item in files:
        item = sanitizer.sanitize(item)
        listStr = listStr + \
            '<li><a href="index.py?id={name}">{name}</a></li>'.format(
                name=item)
    return listStr

'언어 > WEB' 카테고리의 다른 글

[책리뷰] HTTP - 웹 개발자를 위한 웹을 지탱하는 기술  (0) 2019.08.22
CGI 란?  (0) 2019.07.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함