1 Python Essential 2001 4 25 Python Essential

  • Slides: 11
Download presentation
1 Python Essential 세미나 2001. 4. 25(수) Python Essential 세미나 CGI 프로그램 작성법 발표자

1 Python Essential 세미나 2001. 4. 25(수) Python Essential 세미나 CGI 프로그램 작성법 발표자 : 박승기

Python Essential 세미나 ● 기본 CGI 실행환경 과 조건 #! /usr/bin/python 기본 CGI print

Python Essential 세미나 ● 기본 CGI 실행환경 과 조건 #! /usr/bin/python 기본 CGI print "Content-Type : text/plainnn" mtehod print "Hello, Python!" 예제 Display 함수 Hello, Python! 참고서적 1) #! /usr/bin/python è Python interpreter를 자동적으로 호출하기 위한 경로명. 2) print "Content-Type : text/plainnn" 3) 표준출력으로 Content-Type : text/plain 와 두개의 new-line 코드를 출력한다. 3

5 Python Essential 세미나 ● Form Tag와 연결(1 -1) CGI 실행환경 과 조건 기본

5 Python Essential 세미나 ● Form Tag와 연결(1 -1) CGI 실행환경 과 조건 기본 CGI mtehod Form. py #!/usr/bin/python import cgi 예제 Display 함수 print "Content-Type: text/plainnn" 참고서적 The_Form = cgi. Field. Storage() for name in The_Form. keys(): print "Input: " + name + " value: " + The_Form[name]. value print "Finished!"

Python Essential 세미나 ● Form Tag와 연결(1 -2) CGI 실행환경 과 조건 기본 CGI

Python Essential 세미나 ● Form Tag와 연결(1 -2) CGI 실행환경 과 조건 기본 CGI mtehod 예제 Display 함수 Form. html (submission) <form name="form" method="get" action="http: //localhost/cgi-bin/form. py"> <p> email: <input type="text" name="email"> name: <input type="text" name="name"> <input type="submit" value="실행"> </p> </form> 참고서적 실행결과 6

7 Python Essential 세미나 ● Form Tag와 연결(2 -1) CGI 실행환경 과 조건 기본

7 Python Essential 세미나 ● Form Tag와 연결(2 -1) CGI 실행환경 과 조건 기본 CGI Form. py(response) import cgi form=cgi. Field. Storage() mtehod print "Content-type: text/html" 예제 html=""" Display 함수 참고서적 <h 1>Greeting</h 1> <hr> <p>%s</p> <hr>""" if not form. has_key('user'): print html % "WHO ARE YOU? ? ? " else: print html %("Hello, %s. " % form['user']. value)

Python Essential 세미나 ● Form Tag와 연결(2 -2) CGI 실행환경 과 조건 기본 CGI

Python Essential 세미나 ● Form Tag와 연결(2 -2) CGI 실행환경 과 조건 기본 CGI Form. html <h 1><FONT COLOR="green">FORM TEST !!!</FONT></h 1> <hr> mtehod <form method = post action="http: //localhost/cgi-bin/form. py"> 예제 <p> Enter your name : <input type=text name=user> Display 함수 <p> <input type=submit > </form> 참고서적 실행결과 8

9 Python Essential 세미나 ● Display 함수(1 -1) CGI 실행환경 과 조건 기본 CGI

9 Python Essential 세미나 ● Display 함수(1 -1) CGI 실행환경 과 조건 기본 CGI mtehod 예제 Display 함수 참고서적 Module re는 subn함수를 제공하는데 형식은 다음과 같다. Subn(pattern, repl, string) 예) import re res=re. subn(‘ 1234’, ’ 890’, ’ 12345678’) print res 결과) (‘ 8905678’, 1) Def 함수 정의 키워드 >>> def fib(n): . . . a, b = 0, 1. . . while b < n: . . . print b, . . . a, b = b, a+b. . . >>> # 지금 막 정의한 함수를 호출. . . fib(2000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

10 Python Essential 세미나 ● Display 함수(1 -2) CGI 실행환경 과 조건 import re

10 Python Essential 세미나 ● Display 함수(1 -2) CGI 실행환경 과 조건 import re 기본 CGI def Display(Content): mtehod Template. File="template. html" fp=open(Template. File, "r") Template. Input=fp. read() 예제 fp. close() Display 함수 Bad. Template. Exception = " 틀 화일에 문제가 생겼어요" 참고서적 Sub. Result = re. subn("<!-- INSERT CONTENT HERE-->, Content, Template. Input) if Sub. Result[1]==0: raise Bad. Template. Exception print "Content-Type: text/htmlnn" print Sub. Result[0] Display()함수는 하나의 인수(Content)를 받는데 이것은 <-- insert content here--> 위치에 들어갈 문자열이다.

Python Essential 세미나 ● 참고서적 및 사이트 CGI 실행환경 과 조건 기본 CGI mtehod

Python Essential 세미나 ● 참고서적 및 사이트 CGI 실행환경 과 조건 기본 CGI mtehod 예제 Display 함수 참고서적 -Programming Python 2 nd Edition (Mark Lutz, O’REILLY®) -Python 정보광장 http: //www. python. or. kr 11