[python 3.9.1] windows10 cgi 모듈 에러 해결

2021. 2. 9. 20:39Program/Python

생활코딩 이고잉 쌤의 'WEB2 - Python - 7. 활용 - URL query string을 가져오는 방법'

온라인 강의를 보며 열심히 파이썬을 cgi에 연결하여 웹을 제어하는 공부를 하는데...

아무리 강의를 보며 따라해도 발생하는 에러에 골머리가 지끈....

 

www.youtube.com/watch?v=sX-9oG6IPc0&feature=emb_logo

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

첫번째 줄에 shebang을 입력하고~ OK~

(만약 저 코드가 안되신다면 파이썬 설치경로를 직접 입력하거나 환경설정을 하셔야 합니다.)

그다음 타입설정하고 줄바꿈 까지도 OK~

print("""
<!doctype html>
<html>
<head>
  <title>HTML Practice</title>
  <meta charset="utf-8">
</head>
<body>
  <h1><a href="index.py">HTML</a></h1>
  <ol>
    <li><a href="index.py?id=HTML">HTML</a></li>
    <li><a href="index.py?id=CSS">CSS</a></li>
    <li><a href="index.py?id=JavaScript">JavaScript</a></li>
  </ol>

  <h2>{title}</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum..</p>
</body>
</html>
""")

이후 HTML코드를 print("""   """) 안에 넣어서 출력하는 것 까지도 OK~~

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

이후 cgi 모듈을 import하고~ 여기도 OK~

cgi.FiedStorage()를 form 변수에 담는것도 OK~~ 였는데

이고잉쌤이 작성한데로... form변수를 가져오는 코드에서 자꾸 에러가 나는....

 

구글링을 통해 엄청 찾아헤매도 자료들이 넘나 오래되어 그 해답을 찾기 넘나 어려웠는데...

 

결국 해결방법은 python 공식 페이지에서 찾았습니다....

바로 위의 세번째 코드를 아래와 같이 변화하고서야 에러를 해결...

import cgi
form = cgi.FieldStorage()
pageId = form.getvalue("id")

python 3.9.1 버전에서는 form 변수에 담은 cgi.FielStorage( )의 value값을

가져오는 function이 위와같이 입력해야 하는 것으로 업데이트 된듯합니다...

 

결국 수정하여 겨우 수업에 따라가기 위해 완성한 코드입니다.

#!python
print("Content-Type: text/html")
print()
import cgi
form = cgi.FieldStorage()
pageId = form.getvalue("id")
print("""
<!doctype html>
<html>
<head>
  <title>HTML Practice</title>
  <meta charset="utf-8">
</head>
<body>
  <h1><a href="index.py">HTML</a></h1>
  <ol>
    <li><a href="index.py?id=HTML">HTML</a></li>
    <li><a href="index.py?id=CSS">CSS</a></li>
    <li><a href="index.py?id=JavaScript">JavaScript</a></li>
  </ol>

  <h2>{title}</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum..</p>
</body>
</html>
""".format(title=pageId))

 

혹시나 python 에서 cgi 모듈을 impot하여 사용하실 분들이 계시다면

아래의 python 공식 페이지의 가이드를 한번 훓어보시고 사용하시는 것을 추천드립니다.

 

docs.python.org/ko/3/library/cgi.html

 

cgi — Common Gateway Interface support — Python 3.9.1 문서

cgi — Common Gateway Interface support Source code: Lib/cgi.py Support module for Common Gateway Interface (CGI) scripts. This module defines a number of utilities for use by CGI scripts written in Python. Introduction A CGI script is invoked by an HTTP

docs.python.org

 

 

'Program > Python' 카테고리의 다른 글

[python 3.9.1] cgitb 디버깅 모듈  (0) 2021.02.09