WEB2 - Python

활용 - 수정 구현

수업소개

여기서는 Python 웹애플리케이션에서 어떻게 수정 기능을 구현하는가를 살펴봅니다. 


 

강의

 

 

소스코드

변경사항 

update.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>
  <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="description" placeholder="description">{form_default_description}</textarea></p>
      <p><input type="submit"></p>
  </form>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=listStr, form_default_title=pageId, form_default_description=description))

 

process_update.py

#!/usr/local/bin/python3

import cgi, os
form = cgi.FieldStorage()
pageId = form["pageId"].value
title = form["title"].value
description = form['description'].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()

 

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()
    update_link = '<a href="update.py?id={}">update</a>'.format(pageId)
else:
    pageId = 'Welcome'
    description = 'Hello, web'
    update_link = ''
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}
  <h2>{title}</h2>
  <p>{desc}</p>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=listStr, update_link=update_link))

 

댓글

댓글 본문
작성자
비밀번호
  1. Lethesriver
    와 감사합니다!! 해결됐어요
    대화보기
    • 오춘식
      mark position
    • 물속의달
      저도 같은 에러가 나서 당황했는데 도움주셔서 감사합니다.
      대화보기
      • ddojai
        윈도우 환경에서는 opened_file.close()를 호출 안하고 os.rename을 시도하면 에러가 발생하는듯 합니다.

        [Sun Apr 08 15:23:07.678729 2018] [cgi:error] [pid 5476:tid 1272] [client 127.0.0.1:54030] End of script output before headers: process_update.py, referer: http://127.0.0.1......TML
        [Sun Apr 08 15:23:07.679731 2018] [cgi:error] [pid 5476:tid 1272] [client 127.0.0.1:54030] AH01215: Traceback (most recent call last):\r: C:/Bitnami/wampstack-7.1.15-0/apache2/htdocs/process_update.py, referer: http://127.0.0.1......TML
        [Sun Apr 08 15:23:07.679731 2018] [cgi:error] [pid 5476:tid 1272] [client 127.0.0.1:54030] AH01215: File "C:/Bitnami/wampstack-7.1.15-0/apache2/htdocs/process_update.py", line 13, in <module>\r: C:/Bitnami/wampstack-7.1.15-0/apache2/htdocs/process_update.py, referer: http://127.0.0.1......TML
        [Sun Apr 08 15:23:07.679731 2018] [cgi:error] [pid 5476:tid 1272] [client 127.0.0.1:54030] AH01215: os.rename('data/'+pageId, 'data/'+title)\r: C:/Bitnami/wampstack-7.1.15-0/apache2/htdocs/process_update.py, referer: http://127.0.0.1......TML
        [Sun Apr 08 15:23:07.679731 2018] [cgi:error] [pid 5476:tid 1272] [client 127.0.0.1:54030] AH01215: PermissionError: [WinError 32] \xb4\xd9\xb8\xa5 \xc7\xc1\xb7\xce\xbc\xbc\xbd\xba\xb0\xa1 \xc6\xc4\xc0\xcf\xc0\xbb \xbb\xe7\xbf\xeb \xc1\xdf\xc0\xcc\xb1\xe2 \xb6\xa7\xb9\xae\xbf\xa1 \xc7\xc1\xb7\xce\xbc\xbc\xbd\xba\xb0\xa1 \xbe\xd7\xbc\xbc\xbd\xba \xc7\xd2 \xbc\xf6 \xbe\xf8\xbd\xc0\xb4\xcf\xb4\xd9: 'data/HTML' -> 'data/HTML5'\r: C:/Bitnami/wampstack-7.1.15-0/apache2/htdocs/process_update.py, referer: http://127.0.0.1......TML
      버전 관리
      egoing
      현재 버전
      선택 버전
      graphittie 자세히 보기