Skip to content

Instantly share code, notes, and snippets.

@ecarreras
Created July 17, 2017 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecarreras/d40059004897436fb255840397f30379 to your computer and use it in GitHub Desktop.
Save ecarreras/d40059004897436fb255840397f30379 to your computer and use it in GitHub Desktop.
from flask import Flask, request
import time
app = Flask(__name__)
@app.route('/ARequest', methods=['POST'])
def arquest():
print "Writing a post!"
with open('post-{}'.format(time.time()), 'w') as f:
f.write(request.data)
return "Ok", 200
if __name__ == '__main__':
app.run()
@MiquelIR
Copy link

from flask import Flask, request
import xml.dom.minidom
import time

app = Flask(__name__)


def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)


@app.route('/', methods=['GET'])
def getrequest():
    print "get!"
    return "Ok", 200


@app.route('/ARequestStatus', methods=['POST'])
def arequest():
    with open('post-{}'.format(time.time()), 'w') as f:
        f.write(request.data)
    dom = xml.dom.minidom.parseString(request.data)
    id_pet = getText(dom.getElementsByTagName("IdPet")[0].childNodes)
    id_dc = getText(dom.getElementsByTagName("IdDC")[0].childNodes)
    req_status = getText(dom.getElementsByTagName("ReqStatus")[0].childNodes)
    ref = getText(dom.getElementsByTagName("Reference")[0].childNodes)
    if req_status == '0':
        print "SUCCES: The request with ID {} fot the concentrator {} have finished successfully. {}".format(id_pet, id_dc, ref)
    elif req_status == '1':
        print "WORKING: The request with ID {} fot the concentrator {} is still in progress. {}".format(id_pet, id_dc, ref)
    else:
        print "ERROR: The request with ID {} fot the concentrator {} Had a problem during execution. {}".format(id_pet, id_dc, ref)
    return "Ok", 200


@app.route('/AMeterStatus', methods=['POST'])
def ameter():
    with open('post-{}'.format(time.time()), 'w') as f:
        f.write(request.data)
    dom = xml.dom.minidom.parseString(request.data)
    id_pet = getText(dom.getElementsByTagName("IdPet")[0].childNodes)
    id_dc = getText(dom.getElementsByTagName("IdDC")[0].childNodes)
    id_meter = getText(dom.getElementsByTagName("IdMeters")[0].childNodes)
    status = getText(dom.getElementsByTagName("MeterStatus")[0].childNodes)
    e_category = getText(dom.getElementsByTagName("ErrCat")[0].childNodes)
    e_code = getText(dom.getElementsByTagName("ErrCode")[0].childNodes)
    if status == '0':
        print "SUCCES: The request with ID {} fot the concentrator {} have finished successfully".format(id_pet, id_dc)
    elif status == '1':
        print "ERROR: The request with ID {} fot the concentrator {} is still in progress".format(id_pet, id_dc)
    else:
        print "WARNING: The request with ID {} fot the concentrator {} Order " \
              "has been executed but any report after is missing".format(id_pet, id_dc)
    return "Ok", 200


@app.route('/AReport', methods=['POST'])
def areport():
    with open('post-{}'.format(time.time()), 'w') as f:
        f.write(request.data)
    dom = xml.dom.minidom.parseString(request.data)
    id_pet = getText(dom.getElementsByTagName("IdPet")[0].childNodes)
    id_dc = getText(dom.getElementsByTagName("IdDC")[0].childNodes)
    req_status = getText(dom.getElementsByTagName("ReqStatus")[0].childNodes)
    ref = getText(dom.getElementsByTagName("Reference")[0].childNodes)
    if req_status == '0':
        print "SUCCES: The request with ID {} fot the concentrator {} have finished successfully. {}".format(id_pet, id_dc, ref)
    elif req_status == '1':
        print "WORKING: The request with ID {} fot the concentrator {} is still in progress. {}".format(id_pet, id_dc, ref)
    else:
        print "ERROR: The request with ID {} fot the concentrator {} Had a problem during execution. {}".format(id_pet, id_dc, ref)
    return "Ok", 200

if __name__ == '__main__':
    app.run(host='172.26.0.162', port=5656, debug=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment