TypeError发生在Nginx上,但不适用于Flask Server

时间:2016-02-01 02:14:33

标签: python nginx flask pycrypto pbkdf2

我在python中运行api,并通过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,但我仍然认为请求中发送的数据与nginx的处理方式不同,因为它是用uwsgi或烧瓶。

...
  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.pyRNCryptor-python

1 个答案:

答案 0 :(得分:0)

搞定了。

在/ etc / nginx / sites-enabled / test

uwsgi_pass unix:/...;

应该是

uwsgi_pass unix:///...;

原始错误也源于加密文件中使用的密码。密码是从系统的环境变量中检索的。我后来发现Nginx没有任何本地使用环境变量的功能,但解释了一种解决方法here