반응형
Study Site>
https://www.youtube.com/playlist?list=PLuHgQVnccGMDMxfZEpLbzHPZUEwObEaZq
My Git>
https://github.com/ankiwoong/Bitnami_htdocs
Python CGI 작동 영상>
수업 정리>
CRUD에 대하여서 이해해야된다.
또한 생활코딩 에러 중에서 에러코드는 구글링을 하면서 찾는다.
화면은 아래와 같이 3개로 나눠서 보면서 수업을 들었다.
맨 처음 문제였던 것은 실행이 안되었던 부분은 Python에 실행 위치를 못찾아서 벌어진 현상이였고
디렉터리를 맞추자 실행이 되었다.
하루에 조금씩 조금씩 진행하는게 중요하고 멈추지 말고 해야되며 코드를 하나씩 입력하면서 오타와 왜 안되는지를
찾는것이 가장 중요했다.
https://ko.wikipedia.org/wiki/CRUD
CRUD>
Create(생성)
#!C:\Users\ankiwoong\AppData\Local\Programs\Python\Python37\python.exe
import cgi
import os
import view
print("content-type: text/html; charset-utf-8\n")
print()
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="description" placeholder="description"></textarea></p>
<p><input type="submit"></p>
</form>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=view.getList()))
#!C:\Users\ankiwoong\AppData\Local\Programs\Python\Python37\python.exe
import cgi
form = cgi.FieldStorage()
title = form["title"].value
description = form["description"].value
opened_file = open('data/'+title, 'w')
opened_file.write(description)
print('Location: index.py?id='+title)
print()
Read(읽기)
#!C:\Users\ankiwoong\AppData\Local\Programs\Python\Python37\python.exe
import cgi
import os
import view
import html_sanitizer
print("content-type: text/html; charset-utf-8\n")
print()
sanitizer = html_sanitizer.Sanitizer()
form = cgi.FieldStorage()
if 'id' in form:
title = pageId = form["id"].value
description = open('data/'+pageId, 'r').read()
# description = description.replace('<', '<')
# description = description.replace('>', '>')
title = sanitizer.sanitize(title)
description = sanitizer.sanitize(description)
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))
import os
import html_sanitizer
def getList():
sanitizer = html_sanitizer.Sanitizer()
files = os.listdir('\\Bitnami\\wampstack-7.3.13-0\\apache2\\htdocs\\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
Update(갱신)
#!C:\Users\ankiwoong\AppData\Local\Programs\Python\Python37\python.exe
import cgi
import os
import view
print("content-type: text/html; charset-utf-8\n")
print()
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=view.getList(), form_default_title=pageId, form_default_description=description))
#!C:\Users\ankiwoong\AppData\Local\Programs\Python\Python37\python.exe
import cgi
import 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()
Delete(삭제)
#!C:\Users\ankiwoong\AppData\Local\Programs\Python\Python37\python.exe
import cgi
import os
form = cgi.FieldStorage()
pageId = form["pageId"].value
os.remove('data/'+pageId)
# Redirection
print("Location: index.py")
print()
반응형
'Python_Beginer > Study' 카테고리의 다른 글
C/C++, Java, C#, Python 코딩 스타일 표준 규칙 / 지원 도구 (0) | 2022.08.11 |
---|---|
Python Documentation Rules(파이썬 문서화 규칙) (0) | 2022.08.10 |
[HandCoding]손코딩 테스트 (0) | 2020.01.19 |
[Flake 8]Flake 8 (0) | 2019.12.28 |
[PEP 20]The Zen of Python (0) | 2019.12.28 |