반응형

 

python 2.7.5 버전을 사용하다가 python 3.x 버전을 사용하니 has_key() 를 사용하지 못하게 되었다.

따라서, 이 것을 대응 할 수 있는 함수가 바로 in 어떻게 사용하는지 예시로 남긴다.

 

[초기 값] 

d = {'a': 1, 'b': 2}

 

 

[has_key 이용 - 3.x 이전 버전]

if d.has_key('a'):

    print "포함되어 있네"

else:

    print "포함되지 않음"

 

[in 이용 - 3.x 이후 버전]

if 'a' in d:

    print "포함되어 있네"

else:

    print "포함되지 않음"

 

이렇게 사용하면 된다.

누군가가 나와 같은 삽질이 안했으면 하는 바램으로....

반응형
반응형

 

코딩을 하다보니, 문자열 타입에 따라서 인자가 반영되는 것도 있고

아닌 것도 있다.

 

따라서, 인자가 반영 되기 위해서는 해당 문자열의 타입을 확인해야 하는데 그걸 육안으로 하기 쉽지 않으니

아래의 함수를 이용하면 쉽게 bytes, str 인지 확인이 가능하다.

 

print (type(string)) 

 

 

>>> print (type(b'test'))
<class 'bytes'>
>>> print (type('test'))
<class 'str'>
>>> print (type(u'test'))
<class 'str'> 

 

추가로 문자열 convert 방식을 예시로 넣도록 하겠다. (향후 찾는걸 방지..ㅋㅋ)

 

[String To Bytes]

 

bytestr = str.encode(string)

 

[Bytes To String]

 

str = str.decode(bytes)

 

반응형
반응형

 

Windows 7에서는 Unicode 형태로 지원하고 있어서 문자열을 유니코드 또는 바이너리 형으로

변환하여 호출 해야 된다.

 

 

def Get_DLL_Function(self, dll, function)

        handle = kernel32.GetModuleHandleW(dll)

        if handle <= 0:
            print ("[##] GetModuleHandle False\n")
            return False

        print ("[##] \"%s\" GetModudleHandle : 0x%08x\n" % (dll, handle))

        address = kernel32.GetProcAddress(handle,function)

        if address <= 0:
            print ("[*] Error: 0x%08x." % kernel32.GetLastError())

            print ("[##] GetProcAddress \"%s\" False\n" % function)
            kernel32.CloseHandle(handle)
            return False

        print ("[##] \"%s\" GetProcAddress : 0x%08x\n" % (function, address))
        kernel32.CloseHandle(handle)
        return address

 

해당 코드는 크게 다를 것이 없다.

그러나 호출하는 과정에서 문자열을 b"..." 형태로 변환하여 사용하지 않으면 호출되지 않는다.

(u"..." 형태도 있지만 python 3.x 에서부터는 사용하지 못하고 있다.)

http://docs.python.org/3.0/whatsnew/3.0.html

 You can no longer use u"..." literals for Unicode text. However, you must use b"..." literals for binary data.

 

위 내용을 기반으로 b"..."로 변환하니 결과가 나오게 된다.

 

Get_DLL_Function("msvcrt.dll",b"printf") 

 

[결과 값]

 

[##] "msvcrt.dll" GetModudleHandle : 0x77250000

[##] "b'printf'" GetProcAddress : 0x7726c5b9 

 

이걸 얻기 위해 얼마나 삽질을 했던가...

비로소 해결을 했다.~!!!

반응형

'프로그래밍 > Python' 카테고리의 다른 글

python 3.x has_key 없어짐!!!!  (0) 2013.07.23
문자열 타입 확인  (0) 2013.07.23
Windows 7에서 GetModuleHandle 사용하기  (0) 2013.07.12
64bit debugger 만들기  (0) 2013.07.02
python thread context 보기(win7 64bit)  (2) 2013.07.02

+ Recent posts