Advertisement
Guest User

Untitled

a guest
May 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import requests
  2. url="https://palena.sii.cl/DTEWS/CrSeed.jws?WSDL"
  3. headers = {'content-type': 'text/xml; charset=utf-8',
  4. 'Access-Control-Allow-Credentials':'true',
  5. 'SOAPAction':'getSeed'
  6. }
  7.  
  8. body = """
  9. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  10. <soapenv:Header/>
  11. <soapenv:Body>
  12. <getSeed></getSeed>
  13. </soapenv:Body>
  14. </soapenv:Envelope> """
  15.  
  16. response = requests.post(url,data=body,headers=headers)
  17. contenido = response.content
  18.  
  19. salida = contenido.replace("&lt;", "<").replace("&quot;","\"").replace("&gt;",">");
  20.  
  21. original = "<?xml version="+"\""+"1.0"+"\"" + " encoding="+"\""+ "UTF-8" + "\""+"?>"
  22. reemplazo = ""
  23.  
  24. salida = salida.replace(original,reemplazo);
  25.  
  26.  
  27.  
  28. f = open("semilla.xml", "w")
  29. f.write(salida)
  30. f.close()
  31.  
  32.  
  33.  
  34. from xml.dom.minidom import *
  35.  
  36. xmlTree = xml.dom.minidom.parse('semilla.xml')
  37. valorsemilla = ""
  38. for element in xmlTree.getElementsByTagName("SEMILLA"):
  39. valor1 = element.firstChild.nodeValue
  40. valorsemilla = valor1.lstrip('+-0')
  41. doc=Document()
  42. gettoken = doc.createElement("getToken")
  43. item = doc.createElement("item")
  44. semilla = doc.createElement("Semilla")
  45. text = doc.createTextNode(valorsemilla)
  46.  
  47. semilla.appendChild(text)
  48. item.appendChild(semilla)
  49. gettoken.appendChild(item)
  50.  
  51. doc.appendChild(gettoken)
  52.  
  53. contenido = doc.toprettyxml(indent=" ")
  54.  
  55.  
  56. sig = """<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
  57. <SignedInfo>
  58. <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
  59. <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
  60. <Reference URI="">
  61. <Transforms>
  62. <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
  63. </Transforms>
  64. <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
  65. <DigestValue/>
  66. </Reference>
  67. </SignedInfo>
  68. <SignatureValue/>
  69. <KeyInfo>
  70. <KeyValue/>
  71. <X509Data>
  72. <X509Certificate/>
  73. </X509Data>
  74. </KeyInfo>
  75. </Signature>"""
  76.  
  77.  
  78. contenido = contenido.replace('</getToken>',sig+'</getToken>')
  79. #guardo el xml del documento
  80. f = open("gettoken.xml", "w")
  81. f.write(contenido)
  82. f.close()
  83.  
  84.  
  85. import os
  86. os.system("xmlsec1 --sign --privkey-pem key.pem,cert.pem --pwd 'amulen1956' gettoken.xml > gettokensigned.xml")
  87.  
  88. f = open("gettokensigned.xml", "r")
  89. contenido = f.read()
  90. f.close()
  91.  
  92. contenidoa = contenido.replace('<?xml version="1.0"?>','')
  93.  
  94. url="https://palena.sii.cl/DTEWS/GetTokenFromSeed.jws?WSDL"
  95. headers = {'content-type': 'text/xml; charset=utf-8',
  96. 'Access-Control-Allow-Credentials':'true',
  97. 'SOAPAction':'getToken'
  98. }
  99.  
  100.  
  101.  
  102.  
  103. body = """
  104. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  105. <soapenv:Header/>
  106. <soapenv:Body>
  107. <getToken>
  108. <pszXml>
  109. """
  110.  
  111. body = body+"<![CDATA["+'\n'+contenidoa+"]]></pszXml></getToken></soapenv:Body></soapenv:Envelope>"
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. print body
  120.  
  121.  
  122. response = requests.post(url,data=body,headers=headers)
  123. respuesta = response.content
  124.  
  125.  
  126. salida = respuesta.replace("&lt;", "<").replace("&quot;","\"").replace("&gt;",">");
  127.  
  128. original = "<?xml version="+"\""+"1.0"+"\"" + " encoding="+"\""+ "UTF-8" + "\""+"?>"
  129. reemplazo = ""
  130.  
  131. salida = salida.replace(original,reemplazo);
  132.  
  133. print salida
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement