Advertisement
s243a

rest_test.py

Nov 7th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. import bottle
  2. from bottle import route, run, Bottle, request
  3. import sys, os, re, urllib
  4. fcpHost = "127.0.0.1"
  5. import fcp
  6.  
  7. jobs={}
  8. app = Bottle()
  9.  
  10.        
  11. class FreeWorkerFactory:
  12.     job = None #A freenet job
  13.     g = None
  14.     body = None
  15.     OutputType =None
  16.     def __init__(job_or_uri,message=None,afile=None,fcpHost = "127.0.0.1",fcpPort= 9481,aBody=None,OutputType="HTML"):
  17.         if type(job_or_uri) is str:
  18.             if job_or_uri in jobs:
  19.                 self.job=jobs[job_or_uri]
  20.             else:
  21.                 node = fcp.FCPNode(fcpHost,verbosity='fcp.DETAIL',port=fcpPort)
  22.                 val = raw_input(message)
  23.                 ksk = raw_input(job_or_uri)
  24.                 uri = "KSK@" + ksk
  25.                 node.put("KSK@"+ksk, data=val, mimetype="text/plain")
  26.                 self.job = node.get(uri, async=True)
  27.         else:
  28.             self.job=job_or_uri
  29.         self.OutputType=OutputType
  30.         if OutputType == "HTML":
  31.             self.Brk="<br>"
  32.         elif OutputType == "UNIX":
  33.             self.Brk="" #We won't use "\n" since print automatically generates the \n. Write can be used instead if one wants to explicitly use the /n
  34.         self.setBody(aBody)    
  35.     def freenetWorker(self):
  36.         #  Empty Callback: http://echochamber.me/viewtopic.php?t=64825
  37.         #stop_me = lambda *_, **_: None
  38.         def on_data(self):
  39.             # we can poll the job
  40.             while True:
  41.                 if job.isComplete():
  42.                     print "Yay! job complete"
  43.                     body.put(job.wait()) #Returns the completion message.
  44.                     break
  45.                 else:
  46.                     print "Waiting" + datetime.datetime.now()+self.Brk
  47.                     body.put("Waiting" + datetime.datetime.now()+self.Brk)
  48.                     gevent.sleep(0) #This yields control to another greenlet
  49.             self.on_finish() #Maybe we can sequence the greenlets instead of putting this here.
  50.         def on_finish():
  51.             body.put('</body></html>') #There should be a way to seperate these last two lines from this method.
  52.             body.put(StopIteration)
  53.         def header():
  54.             if OutputType == "HTML":
  55.                 if start_respone is not None:
  56.                     start_response('200 OK', [('Content-Type', 'text/html')])
  57.             self.body.put(' ' * 1000)
  58.             if OutputType == "HTML":
  59.                 self.body.put("<html><body><h1>Current Time:</h1>")  
  60.                
  61.         def handle(self,environ=None, start_response=None,aBody=None):
  62.             lambda a: self.start(self,enviorn,start_response)
  63.             #g = Greenlet.spawn(on_data, body)
  64.         def setBody(aBody=None):
  65.             if aBody is None:
  66.                if self.body is None:
  67.                    self.body = queue.Queue()
  68.                    self.g = Greenlet(self.on_data)
  69.             else:
  70.                self.body = aBody
  71.         def start(self,environ=None, start_response=None,aBody=None): # Not sure
  72.             #https://stackoverflow.com/questions/20824218/how-to-implement-someasyncworker-from-bottle-asynchronous-primer
  73.             #g = Greenlet.spawn(current_time, lambda a: on_data(body))
  74.             self.setBody(aBody)
  75.             self.header()
  76.             self.g.start()            
  77.         return self.freenetWorker
  78.  
  79.  
  80.        
  81. @app.post('/KSK/insert')
  82. def rest_test():
  83.     node = fcp.FCPNode(request.json.get('ip'),request.json.get('verbosity=fcp.DETAIL'))
  84.     val = raw_input(request.json.get('message'))
  85.     ksk = raw_input(request.json.get('ksk'))
  86.     uri = "KSK@" + ksk
  87.    
  88.     print "Inserting %s, containing '%s'" % (uri, val)
  89.     # do the put - note that 'data=' inserts a string directly
  90.     # note too that mimetype is optional, defaulting to text/plain
  91.     node.put("KSK@"+ksk, data=val, mimetype="text/plain")
  92.     # ------------------------------------------
  93.     # now, demonstrate asynchronous requests    
  94.     print "Launching asynchronous request"
  95.  
  96.     job = node.get(uri, async=True)
  97.     jobs["KSK@"+ksk]=job    
  98.    
  99.     worker = FreeWorkerFactory(job).freenetWorker()
  100.     worker.start()
  101.     return body      
  102.  
  103.  
  104. app.run(host='localhost', port=8082, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement