A place for things I don't want to forget

Tuesday, October 20, 2009


from twisted.web import server, xmlrpc, http
from twisted.web.server import Site
from twisted.web.static import File
from twisted.web.resource import Resource
from twisted.internet.task import deferLater
from twisted.enterprise import adbapi
from twisted.internet import reactor, threads, defer

import sys
import time
import random
class TestRPC(xmlrpc.XMLRPC):
def __init__(self):
self.allowNone = True

def testDelay(self, results):
print type(results)
return results

def xmlrpc_helloFast(self, test):
return test

def xmlrpc_helloSlow(self, test):
ret = dbpool.runQuery("SELECT tst from test");
ret.addCallback(self.testDelay)
return ret

def insertTst(self, txn, test):
txn.execute("INSERT INTO test (tst) VALUES (?) ", (test,))


def xmlrpc_helloTxn(self, test):
something = dbpool.runInteraction(self.insertTst, test)
return something

class CodeHandler(Resource):
def __init__(self, path):
#self.children = {'test':self.hello()}
self.children = {}
self.path = '/tmp/lol.sql3'

def hello(self):
return(File('/tmp/'))

def authRequest(self, request):
'''
This method should authorize incoming requests for code download.
It should return a deferred object and the callback should either
do all the magic stuff or send a 403 back

For now let's just return true/false.
'''
res = request.getHeader('lol')
if (res == '1234'):
return (True, 'OK')
else:
return (False, '''Just because I don't like you''')

def calcSig(self, path, sleep, request):
time.sleep(random.randint(0,20))
request.setHeader('sig','12983ty12gfoiu12fhn2fy12pf2q')
return (request, path)

def putResults(self, parms):
request = parms[0]
path = parms[1]
print "Served %s" % request.uri
return File(path).render(request)

def _catchFail(self, err):
''' cleanup stuff in case connection is closed on us goes here '''
print "client disconnected: %s" % err


def render(self, request):
self.id = random.random()
print "Serving %s" % request.uri
ret = self.authRequest(request)
if ret[0]:
if request.getHeader('sleep'):
d = threads.deferToThread(self.calcSig, self.path, True, request)
d.addCallback(self.putResults)
request.notifyFinish().addErrback(self._catchFail)
return server.NOT_DONE_YET
request.setHeader('omg', 'hi')
print "Served %s" % request.uri
return File(self.path).render(request)
else:
request.setResponseCode(http.FORBIDDEN)
return "

Access Denied


%s" % ret[1]

def getChild(self, path, request):
return self

if __name__ == '__main__':
root = Resource()
root.putChild('RPC2', TestRPC())
root.putChild('modules', CodeHandler('/tmp'))
factory = Site(root)
dbpool = adbapi.ConnectionPool('sqlite3','/tmp/lol.sql3')
reactor.listenTCP(8888, factory)
reactor.run()