2018년 6월 20일 수요일

python decode mime

import email
import base64

def decode_mime(_msg, _result=[]):
    def _decode(msg, result):
        if msg.is_multipart():
            for item in msg.get_payload():
                _decode(item, result)
        else:
            if 'content-type' in msg:
                _content_type = msg['content-type']
                if 'text/html' in _content_type:
                    decoded_text = base64.decodestring(msg.get_payload())
                    result.append(decoded_text)
    _decode(_msg, _result)

decode_mime(email.message_from_string(_message), result=[])

python HTMLParser

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init(self)
        self.result = list()

    def handle_date(self, data):
        if data is not None:
            self.result.append(data)

obj = MyHTMLParser()
obj.feed(html)
print ' '.join(obj.result)

2018년 5월 28일 월요일

Tornado 에서 static 파일 (javascript, css) 경로 설정

========= 디렉토리 구조 ====================================
Tornado
         |- static
                  |- script (javascript)
                  |- styles (css)
         |- templates
                  |- index.html

========= python 소스   ====================================

import os
import tornado.ioloop
import tornado.web
 
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")
 
def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r"/(.*)", tornado.web.StaticFileHandler, {'path': os.path.join(os.path.dirname(__file__), 'static')}),
        ],
        template_path=os.path.join(os.path.dirname(__file__), "templates"))
 
if __name__ == '__main__':
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

========= index.html  ====================================
<head>
         <script src="scripts/test.js"></script>
         <link rel="stylesheet" href="styles/main.css" />
</head>

2017년 9월 27일 수요일

[Python] cx_Oracle CLOB, BLOB 처리

for _row in cur:
    if isinstance(elem, cx_Oracle.LOB) or isinstance(elem, cx_Oracle.CLOB) or isinstance(
elem, cx_Oracle.BLOB):
data = elem.read()

2017년 3월 8일 수요일

[python] datetime to timestamp, timestamp to datetime convert

import time
from datetime import datetime

if __name__ == '__main__':
   _now = datetime.now()
   print _now
   _timestamp = time.mktime(_now.timetuple())
   print datetime.fromtimestamp(_timestamp)

2016년 12월 22일 목요일

[Python] 기본 encoding 변경

기본 encoding을 utf-8로 변경하기 위해서 site.py 수정
def setencoding():
    encoding = "utf-8"
    .... 

[Python] Windows에 cx_Oracle 설치


  1. OS : Windows 10 64bit
  2. Python : 2.7.12, 64bit
  3. Oracle Instant Client downalod
    • http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
    • 다운로드 -> 압축해제 -> 환경변수 -> 시스템변수 -> Path 추가
  4. cx_Oracle download
    • https://pypi.python.org/pypi/cx_Oracle/5.2.1

docker redmine 설치

docker-composer.yml version: '3.1' services:      redmine:           image: redmine           restart: always           container_na...