Falcon python M2Crypto RSA 错误

Falcon python M2Crypto RSA Error

我们最近开发了一个使用 RSA 的 API,主要问题是在控制台中它可以正常工作,但是一旦进入应用程序的 "venv" 就会显示不同的错误。

Traceback (most recent call last):


File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
    __import__(module)
  File "/root/merci_api/main.py", line 12, in <module>
    import transaction_commit
  File "/root/merci_api/transaction_commit.py", line 9, in <module>
    import cfdi
  File "/root/merci_api/cfdi.py", line 15, in <module>
    from M2Crypto import RSA
  File "/root/merci_api/venv/lib/python3.5/site-packages/M2Crypto-0.25.1-py3.5-linux-x86_64.egg/M2Crypto/__init__.py", line 26, in <module>
    from M2Crypto import (ASN1, AuthCookie, BIO, BN, DH, DSA, EVP, Engine, Err,
  File "/root/merci_api/venv/lib/python3.5/site-packages/M2Crypto-0.25.1-py3.5-linux-x86_64.egg/M2Crypto/SSL/__init__.py", line 24, in <module>
    from M2Crypto.SSL.SSLServer import (ForkingSSLServer, SSLServer,
  File "/root/merci_api/venv/lib/python3.5/site-packages/M2Crypto-0.25.1-py3.5-linux-x86_64.egg/M2Crypto/SSL/SSLServer.py", line 12, in <module>
    from SocketServer import BaseServer, ForkingMixIn, TCPServer, ThreadingMixIn
ImportError: No module named 'SocketServer'

M2Crypto 库已安装在 ubuntu 和 pip 中。

使用RSA的代码是:

def sella_xml(cfdi, numero_certificado, archivo_cer, archivo_pem):
    keys = RSA.load_key(archivo_pem)
    cert_file = open(archivo_cer, 'r')
    cert = base64.b64encode(cert_file.read())
    xdoc = ET.fromstring(cfdi)
    xsl_root = ET.parse('cadenaoriginal_3_3.xslt')
    xsl = ET.XSLT(xsl_root)
    cadena_original = xsl(xdoc)
    digest = hashlib.new('sha256', str(cadena_original)).digest()
    sello = base64.b64encode(keys.sign(digest, "sha256"))
    comp = xdoc.get('Comprobante')
    xdoc.attrib['Sello'] = sello
    xdoc.attrib['Certificado'] = cert
    return ET.tostring(xdoc)

运行 环境的命令:

 /root/merci_api/venv/bin/gunicorn --workers 3 -b localhost:5000 main:app

有什么建议吗?

我做错了什么?

嗯,问题是我们安装的版本上的 M2Crypto 与 Python 3.* 不兼容,所以当我们使用另一个 M2Crypto 版本时,我们还有另一个问题 "SSL v2"解决我们需要这个。

除非你使用 python3 分支 M2Crypto 仍然不支持 py3k。