Created
March 25, 2017 04:30
-
-
Save osvalr/36e189c0ddc776a65788ee9061001ed6 to your computer and use it in GitHub Desktop.
Python SUNAT Example with suds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Originally taken from https://medium.com/@djperalta/python-suds-sunat-example-afc6c37ad426#.mlm9q2lqb | |
from suds.client import Client | |
from suds.wsse import * | |
import requests | |
import base64 | |
import logging | |
logging.basicConfig(level=logging.INFO) | |
logging.getLogger('suds.client').setLevel(logging.DEBUG) | |
#logging.getLogger('suds.wsdl').setLevel(logging.DEBUG) | |
#logging.getLogger('suds.wsse').setLevel(logging.DEBUG) | |
def addSecurityHeader(client, username, password): | |
security = Security() | |
userNameToken = UsernameToken(username, password) | |
timeStampToken = Timestamp(validity=600) | |
security.tokens.append(userNameToken) | |
security.tokens.append(timeStampToken) | |
client.set_options(wsse=security) | |
username = '20600999806MODDATOS' | |
password = 'moddatos' | |
session = requests.session() | |
session.auth = (username, password) | |
WSDL_URL = 'https://www.sunat.gob.pe/ol-ti-itcpgem-beta/billService?wsdl' | |
client = Client(WSDL_URL, faults=False, cachingpolicy=1, location=WSDL_URL) | |
addSecurityHeader(client, username, password) | |
# Send File | |
f = open('/tmp/xml/20600999806-01-F001-1.zip', 'rb') | |
data_file = f.read() | |
get_file = client.service.sendBill("20600999806-01-F001-1.zip", base64.b64encode(str(data_file))) | |
# Save file | |
get_file_decode = base64.b64decode(str(get_file[1])) | |
f = open('/tmp/xml/R-20600999806-01-F001-1.zip', 'w') | |
f.write(get_file_decode) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment