2016-10-21 3 views
4

두 가지 다른 소프트웨어 환경 (환경 A환경 B)과 두 환경 모두에서 PyJWT를 실행하려고합니다. 하나의 환경에서 완벽하게 작동합니다. 환경 A하지만 환경 B에서 실패합니다.PyJWT의 버전을 확인하는 방법은 무엇입니까?

내가 ESjwt.encode() == algorithm로 호출 할 때 내가 환경 B에 받고 있어요 오류 : Algorithm not supported합니다.

나는 왜 환경에서 대답하려고 노력하고있다.이 아니라 환경 B. 두 환경에 서로 다른 버전의 PyJWT가 설치되어있는 것 같습니다. 그러나 환경 B에 설치된 PyJWT의 버전을 확인하는 것이 어렵다. 내가 어떻게 해??

import jwt, cryptography, sys, pkg_resources 

my_private_key = """XXXXX""" 
my_public_key = """YYYYYY""" 
original = {"Hello": "World"} 
print "sys.version = {}".format(str(sys.version)) 

try: 
    print "dir(jwt) = {}".format(str(dir(jwt))) 
except Exception as e: 
    print "Failed to get dir of jwt module: {}".format(e) 

try: 
    print "dir(cryptography) = {}".format(str(dir(cryptography))) 
except Exception as e: 
    print "Failed to get dir of cryptography module: {}".format(e) 

try: 
    print "jwt = {}".format(str(jwt.__version__)) 
except Exception as e: 
    print "Failed to get version of jwt module using .__version: {}".format(e) 
try: 
    print "cryptography = {}".format(str(cryptography.__version__)) 
except Exception as e: 
    print "Failed to get version of cryptography module using .__version: {}".format(e) 

try: 
    print "pkg_resources.require('jwt')[0].version = {}".format(str(pkg_resources.require("jwt")[0].version)) 
except Exception as e: 
    print "Failed to get version of jwt module via pkg_resources: {}".format(e) 

try: 
    print "pkg_resources.require('cryptography')[0].version = {}".format(str(pkg_resources.require("cryptography")[0].version)) 
except Exception as e: 
    print "Failed to get version of cryptography module via pkg_resources: {}".format(e) 

try: 
    print "original = {}".format(str(original)) 
    encoded = jwt.encode(original, my_private_key, algorithm='ES256') 
except Exception as e: 
    print "encoding exception = {}".format(str(e)) 
else: 
    try: 
     print "encoded = {}".format(str(encoded)) 
     unencoded = jwt.decode(encoded, my_public_key, algorithms=['ES256']) 
    except Exception as e: 
     print "decoding exception = {}".format(str(e)) 
    else: 
     print "unencoded = {}".format(str(unencoded)) 

환경에, 인코딩이 성공 :

sys.version = 2.7.12 (default, Sep 1 2016, 22:14:00) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] 
dir(jwt) = ['DecodeError', 'ExpiredSignature', 'ExpiredSignatureError', 'ImmatureSignatureError', 'InvalidAudience', 'InvalidAudienceError', 'InvalidIssuedAtError', 'InvalidIssuer', 'InvalidIssuerError', 'InvalidTokenError', 'MissingRequiredClaimError', 'PyJWS', 'PyJWT', '__author__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'algorithms', 'api_jws', 'api_jwt', 'compat', 'decode', 'encode', 'exceptions', 'get_unverified_header', 'register_algorithm', 'unregister_algorithm', 'utils'] 
dir(cryptography) = ['__about__', '__all__', '__author__', '__builtins__', '__copyright__', '__doc__', '__email__', '__file__', '__license__', '__name__', '__package__', '__path__', '__summary__', '__title__', '__uri__', '__version__', 'absolute_import', 'division', 'exceptions', 'hazmat', 'print_function', 'sys', 'utils', 'warnings'] 
jwt = 1.4.2 
cryptography = 1.5.2 
Failed to get version of jwt module via pkg_resources: jwt 
pkg_resources.require('cryptography')[0].version = 1.5.2 
original = {'Hello': 'World'} 
encoded = eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJIZWxsbyI6IldvcmxkIn0.ciaXCcO2gTqsQ4JUEKj5q4YX6vfHu33XY32g2MNIVEDXHNllpuqDCj-cCrlGPf6hGNifAJbNI9kBaAyuCIwyJQ 
unencoded = {u'Hello': u'World'} 

환경 B에

나는 환경환경 B 모두에서 다음 계측 코드를 실행 인코딩이 실패합니다. 실행중인 PyJWT의 버전을 알 수 없다는 것을 알 수 있습니다.

sys.version = 2.7.12 (default, Sep 1 2016, 22:14:00) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)]" 
dir(jwt) = ['DecodeError', 'ExpiredSignature', 'Mapping', 'PKCS1_v1_5', 'SHA256', 'SHA384', 'SHA512', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'base64', 'base64url_decode', 'base64url_encode', 'binascii', 'constant_time_compare', 'datetime', 'decode', 'encode', 'hashlib', 'header', 'hmac', 'json', 'load', 'signing_methods', 'sys', 'timegm', 'unicode_literals', 'verify_methods', 'verify_signature'] 
dir(cryptography) = ['__about__', '__all__', '__author__', '__builtins__', '__copyright__', '__doc__', '__email__', '__file__', '__license__', '__name__', '__package__', '__path__', '__summary__', '__title__', '__uri__', '__version__', 'absolute_import', 'division', 'print_function', 'sys', 'warnings'] 
Failed to get version of jwt module using .__version: 'module' object has no attribute '__version__' 
cryptography = 1.5.2 
Failed to get version of jwt module via pkg_resources: jwt 
pkg_resources.require('cryptography')[0].version = 1.5.2 
original = {'Hello': 'World'} 
encoding exception = Algorithm not supported 

답변

3

PyJWT .__version__ 속성이 this 커밋에 0.2.2에 출연 : 그러나 PyJWT이 버전은 내가 사용하려고 해요 알고리즘 ES256이 없습니다. pip 패키지를 설치하는 데 사용 된 경우

import pkg_resources 
print pkg_resources.require("jwt")[0].version 

것은, 당신이 리눅스 쉘에서 시도 할 수 :

일반적으로, setuptools에를 통해 설치된 패키지의 버전을 확인하려면 다음 코드를 실행해야합니다 :

pip show jwt | grep Version 
파이썬 내부에서

같은 일이 :

import pip 
print next(pip.commands.show.search_packages_info(['jwt']))['version'] 
+0

당신이 이유를 아십니까 여기서는 나를 위해 일하지 않니? https://gist.github.com/saqib-zmi/ac3d0f2106b76e729cfb54008995181d –

+0

그 이유는 패키지가 * setuptools *를 통해 설치되지 않았기 때문입니다. 이 코드를 사용하면 해당 사례의 버전 만 확인할 수 있습니다. – table

+0

'pip'가 사용 되었다면 버전을 찾는 명령에 대한 답에도 포함되어 있습니다. – table