Example Python Server Side Script




Download Source File

#!/usr/bin/python3

import sys
import cgi
import os
import json

print("Content-type: text/html\n")

form = cgi.FieldStorage()

action = form.getvalue("action", "")
name = form.getvalue("name", "")
_type = form.getvalue("type", "")
index = form.getvalue("index", "")
ID = form.getvalue("ID", "")
category = form.getvalue("category", "")
issued = form.getvalue("issued", "")
expire = form.getvalue("expire", "")
point = form.getvalue("point", "")
forecaster = form.getvalue("forecaster", "")
password = form.getvalue("password", "")
text = form.getvalue("text", "")
variable = form.getvalue("variable", "")

ACCESS_CODE = "test"

if (password != ACCESS_CODE):
    print("Access Denied")
    sys.exit()

oldFile = open("products.json", 'r')
oldData = oldFile.read()
oldFile.close()

oldJSON = []

if (oldData != ""):
    oldLine = oldData.split('\n')
    oldJSON = json.loads(oldData)

N = len(oldJSON)

mode = 0

if (len(oldData) == 0):
    mode = 1

if (action == "NEW"):
    ID = int(ID)

    for i in range(0, N):
        if (oldJSON[i]['product']['ID'] == ID and oldJSON[i]['product']['type'] == _type):
            ID = oldJSON[i]['product']['ID'] + 1

newString = ""
newString = newString + '{"product": {"name": "' + name + '", '
newString = newString + '"type": "' + _type + '", '
newString = newString + '"index": ' + index + ', '
newString = newString + '"ID": ' + ID + ', '
newString = newString + '"category": "' + category + '", '
newString = newString + '"issued": "' + issued + '", '
newString = newString + '"expire": "' + expire + '", '
newString = newString + '"point": "' + point + '", '
newString = newString + '"forecaster": "' + forecaster + '", '
newString = newString + '"text": "' + text + '", '

if (action == "CAN"):
    newString = newString + '"show": false, '
else:
    newString = newString + '"show": true, '

newString = newString + '"variable": ' + variable + '}}'

if (mode == 0):
    if (action == "CON" or action == "CAN"):
        ID = int(ID)

        for i in range(0, N):
            if (oldJSON[i]['product']['ID'] == ID and oldJSON[i]['product']['type'] == _type):
                if (i != N - 1):
                    newString = newString + ','

                newData = oldData.replace(oldLine[i+1], newString)
                break

    elif (action == "DEL"):
        ID = int(ID)

        if (N == 1):
            newData = ""
        else:
            for i in range(0, N):
                if (oldJSON[i]['product']['ID'] == ID and oldJSON[i]['product']['type'] == _type):
                    textKey = oldLine[i+1] + "\n"

                    if (i == N - 1):
                        textKey = ",\n" + oldLine[i+1]

                    newData = oldData.replace(textKey, "")

                    break
    else:
        newData = oldData[0:-2] + ",\n" + newString + "\n]"
else:
    newData = "[\n" + newString + "\n]"

outFile = open("products.json", 'w')
outFile.write(newData)
outFile.close()


Download Source File