python threading.Thread생성시 function argument 사용 주의점

threading.Thread 생성시 function argument 사용할때

name을 구분해주지 않으면 같은 name이 재시도 되지 않는다.
(name을 구분해야 제대로 돌아간다는말)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// 1)  좋은 

def func():
  pass

for i in range(3):
  th = threading.Thread(func())
  th.start()


// 2) 좋은 

def func():
  pass

for i in range(3):
  thName = 't%d' % i
  th = threading.Thread(func(), name=thName)
  th.start()

convert (unicode <-> euc-kr)

python 2.x에 unicode는 언제봐도 항상 어려운거 같다.

example로 여기다가 남기겠음.

#-*- coding: utf-8 -*-

# utf-8 -> euckr
s1 = unicode('파이썬2.x 유니코드 정말 싫다!!', 'utf-8')
s2 = s1.encode('euc-kr')
print s2

# euckr -> utf-8
s3 = unicode(s2, 'euc-kr')
s4 = s1.encode('utf-8')
print s4

how to install django on cygwin

Cygwin is very restrictive when installs packages.

I needed to install django on cygwin.

I will introduce to install django on cygwin.

following is how to install django 1.8.7 with python 2.7 on cygwin 2.3.0 x86_64

1) install cygwin
   recommending category packages are admin, Base, Database, Debug, Devel, Editors, Interpreters, Libs, Python, Math, Security, System, Util, Web

2) get the django source https://www.djangoproject.com/download/1.8.7/tarball/

3) extract Django-1.8.7.tar.gz

4) $ cd Django-1.8.7
    $ python setup.py build

5) $ python setup.py install

6) enjoy the django

Cannot find GMP version 4.1.3 or higher. centos6.7

gcc 버전을 높이기 위해 디펜던시가 걸린 패키지들을 인스톨하는 중

ppl 패키지에서 난관에 부딛혔다.

아래와 같은 메세지가 configure 메세지에서 뜨며 나를 괴롭혔다.

checking for the GMP library version 4.1.3 or above... no
configure: error: Cannot find GMP version 4.1.3 or higher.
GMP is the GNU Multi-Precision library:
see http://www.swox.com/gmp/ for more information.
When compiling the GMP library, do not forget to enable the C++ interface:
add --enable-cxx to the configuration options.

GMP버전은 이미 4.1.3을 넘었지만 똑같은 문구만 반복된다.

삽질과 구글링을 해본결과

configure 실행시 깔린곳의 library와 include를 옵션으로 지정하여 문제를 해결하였다.

[solution]
1. (gmp라이브러리는 --enable-cxx 옵션을 첨부하여 install한다. 이 부분도 솔루션에 포함되는지 않되는지는 확인해봐야 함)
2. $ ./configure --with-gmp-include=/usr/local/include/ --with-gmp-lib=/usr/local/lib


django error 'dict' object has no attribute 'META'

django 에러

symptom:
'dict' object has no attribute 'META'
Request Method:GET
Request URL:http://128.199.227.215:8000/tag/naver/
Django Version:1.8.4
Exception Type:AttributeError
Exception Value:
'dict' object has no attribute 'META'
Exception Location:/usr/local/lib/python2.7/site-packages/django/template/context_processors.py in debug, line 43
Python Executable:/usr/local/bin/python
Python Version:2.7.10
Python Path:
['/home/django/easy',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages']

solution:
뷰쪽(views.py)에 RequestContext 아규먼트가 제대로 들어갔는지 확인한다.

parsing xml on python. unknown encoding

symptom:
SOAP 통신 서버 시뮬레이터에 xml 파서인 ElementTree에 fromString 메서드에서

"ExpatError: unknown encoding" 에러를 발생시켰다.

--------------------------------------------------------------------------------------
  File "/usr/local/lib/python2.6/SocketServer.py", line 322, in finish_request
  File "khubOiServerOri.py", line 320, in response
  File "/usr/local/lib/python2.6/xml/etree/ElementTree.py", line 1245, in feed
    method()
  File "khubOiServerOri.py", line 125, in do_POST
  File "khubOiServerOri.py", line 177, in recvHandler
  File "/usr/local/lib/python2.6/BaseHTTPServer.py", line 329, in handle
    self.handle_one_request()
    self._parser.Parse(data, 0)
  File "/usr/local/lib/python2.6/BaseHTTPServer.py", line 323, in handle_one_request
192.168.151.30 - - [19/Oct/2015 14:14:08] "POST / HTTP/1.1" 200 -
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python2.6/SocketServer.py", line 617, in __init__
    reqDic = parseOi.parseOi(raw)
  File "/home/pis/SIM/lib/parseOi.py", line 37, in parseOi
    root = ET.fromstring(uRawData)
    method()
    rspData = self.response(paramDic)
ExpatError: unknown encoding: line 1, column 30
--------------------------------------------------------------------------------------

클라이언트는 euc-kr세팅으로 SOAP 통신을 하였고

서버 시뮬레이터에서는 UTF-8 세팅이 기본이였다.

모든것을 다 해보진 않았지만 ElementTree에서는 UTF-8이 기본세팅이기에

euc-kr 세팅으로는 파싱이 안되는걸로 추측하고 있다.

solution:
들어오는 스트링을 utf-8로 변환하고 xml header에 encoding을 utf-8로 세팅한다.
1) before
    root = ET.fromstring(rawData)

2) after
    uRawData = unicode(rawData, 'euc-kr').encode('utf-8')

    uRawData = uRawData.replace('euc-kr','utf-8')

    root = ET.fromstring(uRawData)

uwsgi invalid request block size

uwsgi 실행시 아래와 같은 에러가 난다면?
"invalid request block size: 21327 (max 4096)...skip"

solution:
사용하는 uwsgi.ini를 아래와 같이 수정한다.
module = django.core.wsgi:get_wsgi_application()

libpcap 1.7.4 compile error

libpcap-1.7.4 컴파일시 에러 해결

symptom:
[root@T-PISAPP-01 libpcap-1.7.4]# make
gcc -fpic -I.  -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include   -DHAVE_CONFIG_H  -D_
U_="__attribute__((unused))" -g -O2    -c ./pcap-linux.c
gcc -fpic -I.  -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include   -DHAVE_CONFIG_H  -D_
U_="__attribute__((unused))" -g -O2    -c ./pcap-usb-linux.c
gcc -fpic -I.  -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include   -DHAVE_CONFIG_H  -D_
U_="__attribute__((unused))" -g -O2    -c ./pcap-dbus.c
./pcap-dbus.c: In function 'dbus_write':
./pcap-dbus.c:111: error: 'DBUS_ERROR_INIT' undeclared (first use in this function)
./pcap-dbus.c:111: error: (Each undeclared identifier is reported only once
./pcap-dbus.c:111: error: for each function it appears in.)
./pcap-dbus.c: In function 'dbus_activate':
./pcap-dbus.c:165: error: 'DBUS_ERROR_INIT' undeclared (first use in this function)
make: *** [pcap-dbus.o] 오류 1

solution:
[root@libpcap-1.7.4]# ./configure --enable-dbus=no
[root@libpcap-1.7.4]# make

pip error ImportError: cannot import name HTTPSHandler

It'a way to use ssl by compiling python 2.7


it's reason that no openssl, openssl-devel in python with Centos
( if you using ubuntu, maybe no libssl, libssl-dev )

1. yum install openssl openssl-devel -y

2. Get the Python source in http://www.python.org

3. Extract Python source

4. open "Modules/Setup.dist" by vi
   vi Modules/Setup.dist

5. search "SSL"

6. you can find it as follow
#SSL=/usr/local/ssl
#_ssl _ssl.c \
#        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#        -L$(SSL)/lib -lssl -lcrypto

remove '#'

7. Compile the Python source
./configure
make
make install

8. enjoy it

python simple config library

* Python simple config library code
----------------------------------------------------------------------------------
from ConfigParser import SafeConfigParser

class UserConfigParser:
    def __init__(self):
        self.configParser = SafeConfigParser()
        self.sectionName = ''

    # 컨피그 파일을 가져온다
    def getConfigFile(self, fileName):
        self.configParser.read(fileName)

    # "[String]" 형태의 section(subject)를 가져온다.
    def getSection(self, sectionName):
        self.sectionName = sectionName

    def getBoolField(self, fieldName):
        if self.sectionName == '':
            return None
        elif fieldName == '':
            return None

        rawValue = self.configParser.get(self.sectionName, fieldName)
        value = rawValue.rsplit('#',1)[0].strip()

        if value == 'false' or value == 'False' or value == 'FALSE':
            return False
        elif value == 'true' or value == 'True' or value == 'TRUE':
            return True
        else:
            return False

    def getStringField(self, fieldName):
        if self.sectionName == '':
            return None
        elif fieldName == '':
            return None

        rawValue = self.configParser.get(self.sectionName, fieldName)
        value = rawValue.rsplit('#',1)[0].strip()

        if value[0] == "\'" or value[0] == "\"": # 최초 스트링에 \' \" 값을 제거
            value = value[1:]
        if value[-1] == "\'" or value[-1] == "\"": # 마지막 스트링에 \' \" 값을 제거
            value = value[:-1]

        return value
----------------------------------------------------------------------------------


* CONFIG FILE CONTENT (config.cfg)
----------------------------------------------------------------------------------
[SUBJECT]
NAME = JinheeNoh
CITY = Seoul
----------------------------------------------------------------------------------


* Usage
----------------------------------------------------------------------------------
config = UserConfigParser()

config.getConfigFile('./config.cfg')
config.getSection('SUBJECT')
name = config.getStringField('NAME')
city = config.getStringField('CITY')
----------------------------------------------------------------------------------

시스템 ms 시간 가져오기

// 시스템 ms 단위 시간 가져오기
void fnMakeTime(char* strTime){
    char            fmt[32];
    char            buf[32];
    struct timeval  tv;
    struct tm       *tm;

    gettimeofday(&tv, NULL);

    if((tm = localtime(&tv.tv_sec)) == NULL){
        printf("localtime returns NULL\n");
        sprintf(strTime, "%d", 0);
        return;
    }

    strftime(fmt, sizeof(fmt), "%Y%m%d%H%M%S%%03u", tm);
    sprintf(buf, fmt, tv.tv_usec/1000);
    memcpy(strTime, buf, 18);

    printf("fnMakeTime(%s)\n", strTime);
}

delete core files

쉘에서 해당 날짜 여러 개의 코어파일 삭제하기

$ core=`ls -lt | grep "2015-08-10" | grep "core" | awk '{ print $8 }'`

$ rm $core

simple http server in python

CLIENT
-------------------------------------------------------------------------------------------
#-*- coding:euc-kr -*-
import httplib, urllib
import sys

def conn(msg):
    params = urllib.urlencode({'msg':msg})
    headers = {"Content-type": "application/x-www-form-urlencoded",
                "Accept": "text/xml"}
    conn = httplib.HTTPConnection('127.0.0.1','10001')
    conn.request("GET", '/', params, headers)
    rsp = conn.getresponse()
    print rsp.read()
    print 'END'

if __name__ == '__main__':
    if len(sys.argv) == 2:
        msg = sys.argv[1]
        conn(msg)
    else:
        print 'Invalid args'
-------------------------------------------------------------------------------------------



SERVER
-------------------------------------------------------------------------------------------
#-*- coding:euc-kr -*-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ForkingMixIn
from urllib import unquote_plus

class ForkingHTTPServer(ForkingMixIn, HTTPServer):
    pass

class MyHTTPHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        length = int(self.headers.getheader('content-length'))
        raw = self.rfile.read(length)

        msg = unquote_plus(raw.split('=')[1])

        print msg

        self.send_response(200)
        self.send_header('Content-type', 'text/xml')
        self.send_header('Content-length', str(len(msg)))
        self.end_headers()

        self.wfile.write(msg)
        self.wfile.close()

if __name__ == '__main__':
    print 'START ForkingHTTPServer'

    server = ForkingHTTPServer(('127.0.0.1',10001), MyHTTPHandler)
    server.serve_forever()
-------------------------------------------------------------------------------------------