如何判断PyJWT的版本?
How to determine the version of PyJWT?
我有两个不同的软件环境(环境 A 和 环境 B),我正在尝试 运行 PyJWT在这两种环境中。它在一个环境 环境 A 上运行良好,但在 环境 B 上运行失败。
当我使用 algorithm
== ES
调用 jwt.encode()
时,我在 环境 B 上遇到的错误是:Algorithm not supported
.
我想弄清楚为什么它适用于 环境 A 而不是 环境 B。这两个环境似乎安装了不同版本的 PyJWT。但是确定 环境 B 上安装了哪个版本的 PyJWT 对我来说很困难。我该怎么做?
我 运行 环境 A 和 环境 B 上的以下检测代码:
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))
在环境A上,编码成功:
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 上编码失败。你可以看到我无法判断 运行ning 是什么版本的 PyJWT。但是,此版本的 PyJWT 没有我尝试使用的算法 ES256
:
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
PyJWT .__version__
属性出现在 0.2.2
的 this 提交中。
通常,要查找通过 setuptools 安装的软件包的版本,您需要 运行 以下代码:
import pkg_resources
print pkg_resources.require("jwt")[0].version
如果pip
用于安装包,你可以尝试从linux shell:
pip show jwt | grep Version
python 内部的相同内容:
import pip
print next(pip.commands.show.search_packages_info(['jwt']))['version']
我用过
print(jwt.__version__)
而且效果很好。
我有两个不同的软件环境(环境 A 和 环境 B),我正在尝试 运行 PyJWT在这两种环境中。它在一个环境 环境 A 上运行良好,但在 环境 B 上运行失败。
当我使用 algorithm
== ES
调用 jwt.encode()
时,我在 环境 B 上遇到的错误是:Algorithm not supported
.
我想弄清楚为什么它适用于 环境 A 而不是 环境 B。这两个环境似乎安装了不同版本的 PyJWT。但是确定 环境 B 上安装了哪个版本的 PyJWT 对我来说很困难。我该怎么做?
我 运行 环境 A 和 环境 B 上的以下检测代码:
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))
在环境A上,编码成功:
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 上编码失败。你可以看到我无法判断 运行ning 是什么版本的 PyJWT。但是,此版本的 PyJWT 没有我尝试使用的算法 ES256
:
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
PyJWT .__version__
属性出现在 0.2.2
的 this 提交中。
通常,要查找通过 setuptools 安装的软件包的版本,您需要 运行 以下代码:
import pkg_resources
print pkg_resources.require("jwt")[0].version
如果pip
用于安装包,你可以尝试从linux shell:
pip show jwt | grep Version
python 内部的相同内容:
import pip
print next(pip.commands.show.search_packages_info(['jwt']))['version']
我用过
print(jwt.__version__)
而且效果很好。