类型错误发生在Nginx中,而不是Flas

2024-10-06 12:27:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中运行flask后面的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上使用basic python application.py命令运行服务器时,也不会显示该错误。当让nginx和uwsgi处理连接时,我得到上面显示的内部服务器错误。不太清楚发生了什么。所有其他的非加密路由都可以通过。在

更新:使用以下命令在uwsgi中向上运行服务器一级也可以。Nginx仍然没有:

^{pr2}$

更新2:现在得到另一个更具描述性的TypeError,但我仍然认为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是{a2}


Tags: inpytestappflaskhomerequestubuntu
1条回答
网友
1楼 · 发布于 2024-10-06 12:27:05

开始工作了。在

在/etc/nginx/sites enabled/test中

uwsgi_pass unix:/...;

应该是

^{pr2}$

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

相关问题 更多 >