Saludos tengo una sentencia la cual envia diferentes parametos hacia la terminar e invoca un archivo JAR al parecer lo invoca pero al momento de retornar respuesta me genera este error:
File "/var/www/html/facturacion/ventas/views.py" in GrabarVenta
374. tree = ET.XML(string_xml)
File "/usr/lib64/python2.7/xml/etree/ElementTree.py" in XML
1300. parser.feed(text)
File "/usr/lib64/python2.7/xml/etree/ElementTree.py" in feed
1642. self._raiseerror(v)
File "/usr/lib64/python2.7/xml/etree/ElementTree.py" in _raiseerror
1506. raise err
Exception Type: ParseError at /ventas/grabar_venta
Exception Value: syntax error: line 1, column 0
este error se debe a que de mi invocacion debe retornar un string de un xml firmado en JAVA al parecer no lo retorna y me retorna esto al invocarlo:
Usage: java [-options] class [args...](to execute a class)
or java [-options] -jar jarfile [args...]
El comando con el que invoco a java es:
PATH_FIRMA_ABSOLUTA = os.path.join(os.path.dirname(os.path.dirname(__file__)),'static/SRI/mifirma.p12')
PATH_FIRMAR_JAVA = os.path.join(os.path.dirname(os.path.dirname(__file__)),'static/SRI/firmaXadesBes.jar')
file_pk12 = PATH_FIRMA_ABSOLUTA
password = 'Rgberv1792'
JAR_PATH = PATH_FIRMAR_JAVA
JAVA_CMD = 'java'
ds_document = False
#xml_str = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
PATH_XML_ABSOLUTO = os.path.join(os.path.dirname(os.path.dirname(__file__)),path_xml_generado)
tree = etree.parse(str(PATH_XML_ABSOLUTO))
xml_str = etree.tostring(tree, encoding='utf8', method='xml')
command = [
JAVA_CMD,
'-jar',
JAR_PATH,
xml_str,
base64.b64encode(file_pk12),
base64.b64encode(password)
]
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True
)
res = p.communicate()
Actualizacion
Realizando de la siguiente manera:
from lxml import etree
import shlex, subprocess
import base64 # encriptar archivo
from xml.etree import ElementTree as ET
import io
PATH_FIRMA_ABSOLUTA = os.path.join(os.path.dirname(os.path.dirname(__file__)),'static/SRI/betty_elena_romo_vega.p12')
PATH_FIRMAR_JAVA = os.path.join(os.path.dirname(os.path.dirname(__file__)),'static/SRI/firmaXadesBes.jar')
file_pk12 = PATH_FIRMA_ABSOLUTA
password = 'Rgberv1792'
JAR_PATH = PATH_FIRMAR_JAVA
JAVA_CMD = 'java'
ds_document = False
PATH_XML_ABSOLUTO = os.path.join(os.path.dirname(os.path.dirname(__file__)),path_xml_generado)
tree = etree.parse(str(PATH_XML_ABSOLUTO))
xml_str = etree.tostring(tree, encoding='utf8', method='xml')
command = [
JAVA_CMD,
'-jar',
JAR_PATH,
xml_str,
base64.b64encode(file_pk12),
base64.b64encode(password)
]
print command
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
#shell=True
)
res = p.communicate()
# guardar xml firmado
documento_firmado = res[0]
print "DOCUMENTO JAVA FIRMADO"
print documento_firmado
# guardar xml firmado
string_xml = documento_firmado
tree = ET.XML(string_xml)
Funciona locamente en modo deployment con apache y mod_wsgi, pero al subir al servidor Amazon que esta en modo deploy con apache y mod_wsgi me genera el error:
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/html/facturacionelectronica/ventas/views.py" in GrabarVenta
362. stderr=subprocess.STDOUT,
File "/usr/lib64/python2.7/subprocess.py" in __init__
711. errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py" in _execute_child
1327. raise child_exception
Exception Type: OSError at /ventas/grabar_venta
Exception Value: [Errno 2] No such file or directory
Gracias cualquier sugerencia..!!
xml_str
después que Popen escape la cadena. Intenta entrecomillarxml_str
con"
. ¿El comando corre bien directamente desde shell?