'소개와 설치/Jython'에 해당되는 글 1건

  1. 2012.12.02 Mac OS X 또는 Ubuntu 등의 리눅스에 Jython를 설치하고 간단한 테스트하기

Jython은 JVM(자바 가상 기계, Java Virtual Machine) 상에서 동작하는 파이썬 인터프리터이다. 한 때는 JPython이라는 이름으로 불리우기도 했는데, 지금은 더 짧은 이름 Jython으로 통한다.

리녹스 시스템에 슈퍼유저(su) 권한으로 Jython을 러치하면 캐쉬 디렉토리의 설정 문제로 약간은 다소 귀찮은 일이 발샐한다. 여기서는 이런 문제도 해결하고 Tomcat과 연동하여 PyServlet을 동자기켜 보기로 한다.


* Jython 2.5.3 다운로드

Jython의 현재 최신 릴리즈는 2.5.3이다 Jython 홈페이지 http://jythom.org 에서 jython-installer-2.5.3 을 다운받는다. 다운받은 파일을 적당한 곳으로 옮기고, 터미널에서 그 폴더로 체인지 디렉토리(cd)하여 다음 java 명령으로 jython을 설치한다.

$ sudo java -jar jython-installer-2.5.3.jar






이 글에서는 Jython을 /usr/local/jython-2.5.3에 서리한 것으로 간주하고 계속 진행한다.

/etc/profile에 환경변수 JYTHON_HOME과 PATH를 잡아준다. (다음 두 줄 추가)

$ sudo vi /etc/profile

JYTHON_HOME=/usr/local/jthon-2.5.3; export JYTHON_HOME

PATH=$JYTHON_HOME/bin"$PATH; export PATH


변경된 환경변수 JYTHON_HOME과 PATH를 현재의 셀에 적용시킨다.

$ source /etc/profile



* jython 인터프리터 실행

jython 명령을 사용하면 캐쉬 디렉토리 관련 에러가 뜨면서 실행은 된다.


$ jython hello.py
*sys-package-mgr*: can't create package cache dir, '/usr/local/jython-2.5.3/cachedir/packages'
Hello, world!

이제 이 문제를 해결해 보자. 해결하는데 두 가지 방법이 있다. 하나는 jython 명령을 내릴 때 -D 옵션을 쓰는 것이도, 다른 하나는 ~/.jython 파일에서 캐쉬 디렉토리를 잡아주는 방법이다.

(참고. 전체 경로에 ~ 나 $HOME 은 적어봐야 jython이 인식하지 못한다.)

$ jython -Dpython.cachedir=전체경로 hello.py
Hello, world!


다음 한 줄로 된 파일 ~/.jython 을 작성한다.

$ vi ~/.jython

python.cachedir=전체경로


$ jython hello.py
Hello, world!



PyServlet 설정 및 실행

* web.xml 파일에 추가될 내용

    <servlet>
        <servlet-name>PyServlet</servlet-name>
        <servlet-class>org.python.util.PyServlet</servlet-class>
        <init-param>
         <param-name>python/home</param-name>
         <param-value>/opt/usr/local/jython-2.5.3</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>PyServlet</servlet-name>
        <url-pattern>*.py</url-pattern>
    </servlet-mapping>



* 수정 전 PyServlet: enter.py

import javax.servlet.http as http

class enter(http.HttpServlet):
  def doPost(self, request, response):
    session = request.getSession(1)
    counter = session.getAttribute("counter")
    try:
        counter = int(counter)
    except: # counter is None
        counter = 0
    counter += 1
    session.setAttribute("counter", counter)
    response.contentType = "text/html; charset=utf-8"
    out = response.outputStream print >> out, """The string you entered is: %s. <br />
You have played this game %s times.\n<hr>""" % (
request.getParameter("thing"), counter)
    self.doGet(request, response)

  def doGet(self, request, response):
    response.contentType = "text/html; charset=utf-8"
    out = response.outputStream print >> out, """Enter a string: <form method="post">
<input type="text" name="thing">
<input type="submit">
</form>"""



* Ubuntu에서 Tomcat 6 & Jython 2.5.3을 사용하여 수정전 enter.py를 실행한 장면




* 수정 후 PyServlet: enter.py (utf-8 한글 파라미터 처리를 위해 파란색 부분만 수정됨)

# -*- encoding: utf-8 -*-

import javax.servlet.http as http

class enter(http.HttpServlet):

  def doPost(self, request, response):
    session = request.getSession(1)
    counter = session.getAttribute("counter")
    try:
        counter = int(counter)
    except: # counter is None
        counter = 0
    counter += 1
    session.setAttribute("counter", counter)
    request.setCharacterEncoding("utf-8")
    response.contentType = "text/html; charset=utf-8"
    out = response.outputStream
    print >> out, """The string you entered is: %s. <br />
You have played this game %s times.\n<hr>""" % (
request.getParameter("thing").encode("utf-8"), counter)
    self.doGet(request, response)

  def doGet(self, request, response):
    response.contentType = "text/html; charset=utf-8"
    out = response.outputStream
    print >> out, """Enter a string: <form method="post">
<input type="text" name="thing">
<input type="submit">
</form>"""


* Ubuntu에서 Tomcat 6 & Jython 2.5.3을 사용하여 수정된 enter.py를 실행한 장면







Posted by Scripter
,