-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_consultaCnpj2.py
322 lines (261 loc) · 11.2 KB
/
http_consultaCnpj2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- encoding: utf-8 -*-
import re
import urllib2, httplib, socket
import os.path
import logging
import libxml2
import xmlsec
import suds.bindings
import xml.etree.ElementTree as ET
from suds.sax.text import Raw
from suds.client import Client
from uuid import uuid4
from lxml import etree
from suds.transport.http import HttpTransport, Reply, TransportError
from suds.plugin import MessagePlugin
from suds.sax.element import Element as E
from suds.sax.attribute import Attribute
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
class base_nfse(object):
def __init__(self, arquivo, senha):
self.arquivo = arquivo
self.senha = senha
def _checar_certificado(self):
if not os.path.isfile(self.arquivo):
raise Exception('Caminho do certificado nao existe.')
def _inicializar_cripto(self):
libxml2.initParser()
libxml2.substituteEntitiesDefault(1)
xmlsec.init()
xmlsec.cryptoAppInit(None)
xmlsec.cryptoInit()
def finalizar_cripto(self):
xmlsec.cryptoShutdown()
xmlsec.cryptoAppShutdown()
xmlsec.shutdown()
libxml2.cleanupParser()
def _save_pfx_certificate(self):
pfx_tmp = '/tmp/' + uuid4().hex
arq_temp = open(pfx_tmp, 'w')
arq_temp.write(base64.b64decode(self.certificate))
arq_temp.close()
return pfx_tmp
def converte_pfx_pem(self, pfx_stream, senha):
try:
certificado = crypto.load_pkcs12(pfx_stream, senha)
privada = crypto.dump_privatekey(crypto.FILETYPE_PEM,
certificado.get_privatekey())
certificado = crypto.dump_certificate(crypto.FILETYPE_PEM,
certificado.get_certificate())
except Exception as e:
if len(e.message) == 1 and len(e.message[0]) == 3 and \
e.message[0][2] == 'mac verify failure':
raise Exception('Senha inválida')
raise
return certificado, privada
def render(self, base_path, template_path, **kwargs):
#import pudb; pu.db
env = Environment(loader=FileSystemLoader(os.path.join(base_path, 'templates')))
env.filters["normalize"] = filters.normalize_str
env.filters["format_percent"] = filters.format_percent
env.filters["format_datetime"] = filters.format_datetime
env.filters["format_date"] = filters.format_date
template = env.get_template(template_path)
# TODO Remover espaços e possíveis tags vazias
xml = template.render(**kwargs)
parser = etree.XMLParser(remove_blank_text=True, remove_comments=True)
elem = etree.fromstring(xml, parser=parser)
return etree.tostring(elem)
def assina_xml(self, xml, reference, arquivo, chave):
self._checar_certificado()
self._inicializar_cripto()
try:
doc_xml = libxml2.parseMemory(
xml.encode('utf-8'), len(xml.encode('utf-8')))
signNode = xmlsec.TmplSignature(doc_xml, xmlsec.transformInclC14NId(),
xmlsec.transformRsaSha1Id(), None)
doc_xml.getRootElement().addChild(signNode)
refNode = signNode.addReference(xmlsec.transformSha1Id(),
None, reference, None)
refNode.addTransform(xmlsec.transformEnvelopedId())
refNode.addTransform(xmlsec.transformInclC14NId())
keyInfoNode = signNode.ensureKeyInfo()
keyInfoNode.addX509Data()
dsig_ctx = xmlsec.DSigCtx()
chave = xmlsec.cryptoAppKeyLoad(filename=str(arquivo),
format=xmlsec.KeyDataFormatPkcs12,
pwd=str(chave),
pwdCallback=None,
pwdCallbackCtx=None)
dsig_ctx.signKey = chave
dsig_ctx.sign(signNode)
status = dsig_ctx.status
dsig_ctx.destroy()
if status != xmlsec.DSigStatusSucceeded:
raise RuntimeError(
'Erro ao realizar a assinatura do arquivo; status: "' +
str(status) +
'"')
NAMESPACE_SIG = 'http://www.w3.org/2000/09/xmldsig#'
xpath = doc_xml.xpathNewContext()
xpath.xpathRegisterNs('sig', NAMESPACE_SIG)
certificados = xpath.xpathEval(
'//sig:X509Data/sig:X509Certificate')
for i in range(len(certificados) - 1):
certificados[i].unlinkNode()
certificados[i].freeNode()
xml = doc_xml.serialize()
return xml
finally:
doc_xml.freeDoc()
#self._finalizar_cripto() # erro : urllib2.URLError: <urlopen error _ssl.c:320: Invalid SSL protocol variant specified.>
def valida_schema(self, xml, arquivo_xsd):
'''Função que valida um XML usando lxml do Python via arquivo XSD'''
# Carrega o esquema XML do arquivo XSD
xsd = etree.XMLSchema(file = arquivo_xsd)
# Converte o XML passado em XML do lxml
xml = etree.fromstring(str(xml))
# Verifica a validade do xml
erros = []
if not xsd(xml):
# Caso tenha erros, cria uma lista de erros
for erro in xsd.error_log:
erros.append({
'message' : erro.message,
'domain' : erro.domain,
'type' : erro.type,
'level' : erro.level,
'line' : erro.line,
'column' : erro.column,
'filename' : erro.filename,
'domain_name': erro.domain_name,
'type_name' : erro.type_name,
'level_name' : erro.level_name
})
print "erro %s, linha %s" % (erro.message, erro.line)
# Retorna os erros, sendo uma lista vazia caso não haja erros
return erros
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
def https_open(self, req):
#Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host,
key_file=self.key,
cert_file=self.cert)
class HTTPSClientCertTransport(HttpTransport):
def __init__(self, key, cert, *args, **kwargs):
HttpTransport.__init__(self, *args, **kwargs)
self.key = key
self.cert = cert
def u2open(self, u2request):
"""
Open a connection.
@param u2request: A urllib2 request.
@type u2request: urllib2.Requet.
@return: The opened file-like urllib2 object.
@rtype: fp
"""
tm = self.options.timeout
url = urllib2.build_opener(HTTPSClientAuthHandler(self.key, self.cert))
if self.u2ver() < 2.6:
socket.setdefaulttimeout(tm)
return url.open(u2request)
else:
return url.open(u2request, timeout=tm)
class EnvelopeFixer(MessagePlugin):
def sending(self, context):
# removendo prefixo
context.envelope = re.sub( 'ns[0-9]:', '', context.envelope )
context.envelope = re.sub( '<SOAP-ENV:Header/>', '', str(context.envelope) )
context.envelope = re.sub( '</VersaoSchema>', '</MensagemXML>', str(context.envelope) )
context.envelope = re.sub( '<VersaoSchema>', '<VersaoSchema>1</VersaoSchema><MensagemXML>', str(context.envelope) )
return context.envelope
def marshalled(self, context):
#print context.envelope.str()
envelope = context.envelope
envelope.name = 'Envelope'
envelope.setPrefix('soap12')
envelope.nsprefixes = {
'xsi' : 'http://www.w3.org/2001/XMLSchema-instance',
'soap12': 'http://www.w3.org/2003/05/soap-envelope',
'xsd' : 'http://www.w3.org/2001/XMLSchema'
}
body_ele = envelope.getChildren()[1]
body_ele.setPrefix("soap12")
consulta = envelope.getChildren()[1][0]
consulta.set("xmlns", "http://www.prefeitura.sp.gov.br/nfe")
return Raw(context)
# These lines enable debug logging; remove them once everything works.
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)
certificado = '/home/tas/clientes_server/ats/certificado_ats.pfx'
chave = 'senha_cert'
host = 'nfe.prefeitura.sp.gov.br'
uri = '/ws/lotenfe.asmx?wsdl'
cert_temp = open(certificado, 'r').read()
pfx_tmp = '/tmp/' + uuid4().hex
arq_temp = open(pfx_tmp, 'w')
arq_temp.write(cert_temp)
arq_temp.close()
suds.bindings.binding.envns = ('SOAP-ENV',
'http://www.w3.org/2003/05/soap-envelope')
base = base_nfse(pfx_tmp, chave)
t = HTTPSClientCertTransport('/home/tas/clientes_server/ats/key2.pem',
'/home/tas/clientes_server/ats/cert.pem')
envelope = EnvelopeFixer()
c = Client('https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx?wsdl',
location='https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx',
timeout=300,
transport = t, plugins=[envelope])
# carregar xml do arquivo
#xml = open('/home/tas/consulta_cnpj.xml','r')
#xml_send = xml.read()
xml_send = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><p1:PedidoConsultaCNPJ xmlns:p1=\"http://www.prefeitura.sp.gov.br/nfe\"><Cabecalho Versao=\"1\"><CPFCNPJRemetente><CNPJ>08123456000111</CNPJ></CPFCNPJRemetente></Cabecalho><CNPJContribuinte><CNPJ>64123456000114</CNPJ></CNPJContribuinte></p1:PedidoConsultaCNPJ>"
xml_send = Raw(xml_send)
reference = ""
xml_signed = base.assina_xml(xml_send, reference, pfx_tmp, str(chave))
arq_temp = open('/home/tas/xml_assinado.xml', 'w')
arq_temp.write(xml_signed)
arq_temp.close()
"""
message = \
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"\
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""\
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"\
"<soap12:Body>" + xml_send + \
"</soap12:Body>"\
"</soap12:Envelope>"
"""
#TODO - arrumar para pasta do sistema
valida_schema = base.valida_schema(xml_signed, '/home/tas/doc/fiscal/NFSe_SP/schemas/nfse/PedidoConsultaCNPJ_v01.xsd')
if len(valida_schema):
erros = "Erro(s) no XML: \n"
for erro in valida_schema:
erros += erro['type_name'] + ': ' + erro['message'] + '\n'
raise ValueError(erros)
print xml_signed
xml_pronto = xml_signed
arq_temp = open('/home/tas/xml_soap.xml', 'w')
arq_temp.write(xml_pronto)
arq_temp.close()
#import pudb;pu.db
try:
x = c.service.ConsultaCNPJ(xml_pronto)
finally:
base.finalizar_cripto()
arq_temp = open('/home/tas/retorno.xml', 'w')
arq_temp.write(x)
arq_temp.close()
print x