Skip to content

Instantly share code, notes, and snippets.

@btownrippleman
Last active June 24, 2020 04:37
Show Gist options
  • Select an option

  • Save btownrippleman/11c73eb8e14059f79b756f82737ac7a5 to your computer and use it in GitHub Desktop.

Select an option

Save btownrippleman/11c73eb8e14059f79b756f82737ac7a5 to your computer and use it in GitHub Desktop.
import cherrypy
import random
import string
import json
import os
class HelloWorld(object):
@cherrypy.expose
def index(self):
return "Hello World!"
def randomString(self,stringLength=8):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))
def data_file_initalizer():
filename = "data_file.json"
if not os.path.exists(filename):
with open(filename,"w+") as f:
json.dump({}, f)
@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def post(self,request):
data = {}
filename = "data_file.json"
with open(filename) as read_file:
data = json.load(read_file)
data[self.randomString(20)] = json.loads(request)
print (request)
with open(filename, 'w') as json_file:
json.dump(data, json_file)
return (data)
HelloWorld.data_file_initalizer()
cherrypy.quickstart(HelloWorld())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment