2009년 12월 31일 목요일

구글 앱 엔진, 텍스트 파일 열기


하드의 텍스트 파일을 열어서 스트링 혹은 데이터를 가져올때 예제 코드



from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from util.sessions import Session

class MainPage(webapp.RequestHandler):
def get(self):
session = Session()
if 'words' in session:
words = session['words']
else:
words = 'no words'

self.response.out.write('<html><body>')
self.response.out.write('<form action="/" method="post" enctype="multipart/form-data">')
self.response.out.write('Upload File: <input type="file" name="txt"><br>')
self.response.out.write('<input type="submit" name="submit" value="Submit">')
self.response.out.write(str(words))
self.response.out.write('</form>')
self.response.out.write('</body></html>')

class Study(webapp.RequestHanlder):
def post(self):
words = self.request.get('txt')
self.response.out.write(words)

application = webapp.WSGIApplication([('/', MainPage)],
('/study', Study)
], debug=True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()

2009년 12월 30일 수요일

python에서 html에서 읽어온 string 한글 검색

urlopen으로 읽어온 스트링에서 한글 검색하는 코드이다.

# -*- coding: utf-8 -*-
import urllib
import re

class MyOpener(urllib.FancyURLopener):
version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'

def Translate(eng):
urllib._urlopener = MyOpener()

f = urllib.urlopen('http://www.google.co.kr/dictionary?langpair=en|ko&q='+eng+'&hl=ko&aq=f')
html = f.read()
str = unicode('영어 > 한국어','euc-kr').encode('utf-8')
pos_ko = html.find(str)
str = unicode('영어 사전','euc-kr').encode('utf-8')
pos_en = html.find(str, pos_ko)


if __name__ == '__main__':
Translate('TEST')

2009년 12월 29일 화요일

Hello WebApp 분석

webapp 어플리케이션은 세가지 파트로 구성되어있다:

  • 요구를 처리하고 반응하는 하나 혹은 그 이상의 RequestHandler 클래스
  • URL 기반의 핸들러로 들어온 요구를 라우트하는 WSGIApplication 인스턴스
  • CGI adaptor를 사용하여 WSGIApplication을 실행하는 메인 루틴

webapp 어플리케이션에 어울리게 기존의 helloworld.py 를 수정하여 다음과 같이 다시 코딩해보자:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')

application
= webapp.WSGIApplication(
[('/', MainPage)],
debug
=True)

def main():
run_wsgi_app
(application)

if __name__ == "__main__":
main
()

run_wsgi_app(application)

Runs a WSGI application in App Engine's CGI environment. This is comparable to using a WSGI-to-CGI adaptor such as the one provided by the wsgirefmodule of the Python standard library, but has several advantages: run_wsgi_app() automatically detects whether the application is running in the development server, and if so outputs errors to the browser in addition to the log. run_wsgi_app() also allows App Engine to send data output by the handler prior to the error, which wsgiref.handlers.CGIHandler doesn't do.

Arguments:

application
A WSGI application object, such as webapp.WSGIApplication.

The WSGIApplication Class

The google.appengine.ext.webapp package provides the following classes:

class WSGIApplication(url_mapping, debug=False)

A WSGI-compatible application that maps URL paths to RequestHandler classes. An App Engine CGI script creates a WSGIApplication object, then runs it using a WSGI CGI handler such as run_wsgi_app().

Arguments:

url_mapping
A mapping of URL paths to request handlers, as a list of tuples where each tuple is a path-to-handler mapping. The first element of the tuple specifies the URL path as a string. The second element of the tuple is the constructor of a request handler class, a subclass of RequestHandler.
debug
If True, the application runs in "debug mode." Primarily, this means that the request handler's handle_exception() method is called with debug_mode=Truewhen the handler raises an exception, which prints debugging information to the web browser.

출처