TypeError 发生在 Nginx 而不是 Flask Server
TypeError happens with Nginx but not with Flask Server
我 运行 api 在 python 的 flask 后面,连接是通过 nginx 和 uwsgi 处理的。
一些 api 路由使用 pycrypto 但在端口 80 上使用 nginx 时出现关于 pycrypto 源中 this line 的错误。
完整的追溯是:
Traceback (most recent call last):
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./application.py", line 113, in new_user
decrypted_json = crypt.decrypt_json(encrypted)
File "./crypto_module/crypt.py", line 201, in decrypt_json
decrypted_text = cryptor.decrypt(data_to_decrypt, PASSWORD)
File "./crypto_module/crypt.py", line 112, in decrypt
encryption_key = self._pbkdf2(password, encryption_salt, iterations=iterations)
File "./crypto_module/crypt.py", line 184, in _pbkdf2
return KDF.PBKDF2(password, salt, dkLen=key_length, count=iterations, prf=lambda p, s: hmac.new(p, s, hashlib.sha256).digest())
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/Crypto/Protocol/KDF.py", line 110, in PBKDF2
password = tobytes(password)
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/Crypto/Util/py3compat.py", line 85, in tobytes
return ''.join(s)
TypeError
出于某种原因从未具体提及 TypeError。
当 运行 服务器在默认端口 5000 上使用基本 python application.py
命令时,错误也不会显示。当让 nginx 和 uwsgi 处理连接时,我得到上面显示的内部服务器错误。不太确定发生了什么。所有其他非加密路由都正常。
更新: 运行 使用以下命令在 uwsgi 中向上一级的服务器也可以工作。 Nginx 还是不行:
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi
更新 2: 现在又出现了一个更具描述性的 TypeError,但我仍然认为请求中发送的数据处理方式与 way/correctly 不同nginx 与 uwsgi 或 flask 一样。
...
File "./crypto_module/crypt.py", line 202, in <lambda>
return KDF.PBKDF2(password, salt, dkLen=key_length, count=iterations, prf=lambda p, s: hmac.new(p, s, hashlib.sha256).digest())
File "/usr/lib/python2.7/hmac.py", line 133, in new
return HMAC(key, msg, digestmod)
File "/usr/lib/python2.7/hmac.py", line 68, in __init__
if len(key) > blocksize:
TypeError: object of type 'NoneType' has no len()
我还应该注意到 crypt.py
是 RNCryptor-python
成功了。
在/etc/nginx/sites-enabled/test
uwsgi_pass unix:/...;
应该是
uwsgi_pass unix:///...;
最初的错误也源于加密文件中使用的密码。密码是从系统的环境变量中检索到的。后来我发现 Nginx 没有任何本地使用环境变量的功能,但解释了一种解决方法 here.
我 运行 api 在 python 的 flask 后面,连接是通过 nginx 和 uwsgi 处理的。
一些 api 路由使用 pycrypto 但在端口 80 上使用 nginx 时出现关于 pycrypto 源中 this line 的错误。
完整的追溯是:
Traceback (most recent call last):
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./application.py", line 113, in new_user
decrypted_json = crypt.decrypt_json(encrypted)
File "./crypto_module/crypt.py", line 201, in decrypt_json
decrypted_text = cryptor.decrypt(data_to_decrypt, PASSWORD)
File "./crypto_module/crypt.py", line 112, in decrypt
encryption_key = self._pbkdf2(password, encryption_salt, iterations=iterations)
File "./crypto_module/crypt.py", line 184, in _pbkdf2
return KDF.PBKDF2(password, salt, dkLen=key_length, count=iterations, prf=lambda p, s: hmac.new(p, s, hashlib.sha256).digest())
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/Crypto/Protocol/KDF.py", line 110, in PBKDF2
password = tobytes(password)
File "/home/ubuntu/test/testvenv/local/lib/python2.7/site-packages/Crypto/Util/py3compat.py", line 85, in tobytes
return ''.join(s)
TypeError
出于某种原因从未具体提及 TypeError。
当 运行 服务器在默认端口 5000 上使用基本 python application.py
命令时,错误也不会显示。当让 nginx 和 uwsgi 处理连接时,我得到上面显示的内部服务器错误。不太确定发生了什么。所有其他非加密路由都正常。
更新: 运行 使用以下命令在 uwsgi 中向上一级的服务器也可以工作。 Nginx 还是不行:
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi
更新 2: 现在又出现了一个更具描述性的 TypeError,但我仍然认为请求中发送的数据处理方式与 way/correctly 不同nginx 与 uwsgi 或 flask 一样。
...
File "./crypto_module/crypt.py", line 202, in <lambda>
return KDF.PBKDF2(password, salt, dkLen=key_length, count=iterations, prf=lambda p, s: hmac.new(p, s, hashlib.sha256).digest())
File "/usr/lib/python2.7/hmac.py", line 133, in new
return HMAC(key, msg, digestmod)
File "/usr/lib/python2.7/hmac.py", line 68, in __init__
if len(key) > blocksize:
TypeError: object of type 'NoneType' has no len()
我还应该注意到 crypt.py
是 RNCryptor-python
成功了。
在/etc/nginx/sites-enabled/test
uwsgi_pass unix:/...;
应该是
uwsgi_pass unix:///...;
最初的错误也源于加密文件中使用的密码。密码是从系统的环境变量中检索到的。后来我发现 Nginx 没有任何本地使用环境变量的功能,但解释了一种解决方法 here.