[Twisted-Python] alternative to tac

Werner Thie wthie at thiengineering.ch
Tue Oct 7 05:18:28 EDT 2008


Hi Vasil

I use something along the code below whoch serves dual purpose, use it 
with twistd -noy xxx.tac and also python xxx.tac.

I was undecided almost a year ago whether I should use config files too, 
but then found that the .tac files work great as config files too.

I mainly develop on MSW XP and Vista and routinely deploy to OpenBSD.

HTH, Werner

PS: some refs in the code are generalized

import sys, os, gc

from twisted.enterprise import adbapi

from twisted.application import service, strports, internet
from twisted.cred import portal, credentials, checkers
from twisted.internet import protocol, reactor
#from twisted.conch import manhole, manhole_ssh
from twisted.python import log

from nevow import appserver, inevow

from theproject import web, i18n, plugin, ourguard

if sys.platform != 'win32':
   import psyco
   psyco.full()

   UID = 67
   GID = 67
   os.environ['PYTHON_EGG_CACHE'] = '/tmp'

DB_DRIVER = "MySQLdb"
DB_ARGS = {
     'host': 'xxx.xxx.xxx.xxx',
     'db': 'somedb',
     'user': 'someuser',
     'passwd': 'somepwd',
     'cp_reconnect': True
     }

application = service.Application("appname")

print
print 'available userstores:'
for userstore in plugin.userstores():
   print userstore.name

UserstoreName  = 'dbonly'
#UserstoreName  = 'memonly'
[Userstore] = [x for x in plugin.userstores() if x.name == UserstoreName]
print 'selected: %s' % Userstore.name

userstore = Userstore(DB_DRIVER, **DB_ARGS)

#additional style locations to be put into second arg, third arg is 
#redirect to in case of not knowing where to go
theportal = portal.Portal(web.Realm(web.Root('style_loc', [], 
'www.google.com'), userstore))

theportal.registerChecker(checkers.AllowAnonymousAccess(), 
credentials.IAnonymous)

thersrc = ourguard.TheSessionWrapper(theportal, mindFactory=web.Mind)
site = appserver.NevowSite(thersrc)

webserver = strports.service("tcp:7999", site)
webserver.setServiceParent(application)

def main():
   log.startLogging(sys.stdout)
   reactor.listenTCP(7999, site) #start only the web svc when debugging
   reactor.run()
##  print gc.get_objects()

if __name__ == '__main__':
   main()
##  import cProfile, pstats
##  p = cProfile.Profile()
##  p.run('main()')
##  s = pstats.Stats(p)
##  s.strip_dirs()
##  s.sort_stats('call', 'cum', 'name').print_stats().print_callers()



Vasil Vangelovski wrote:
> Which is the simplest way to put a service under and application to
> listen on a port and behave similarly to the way it behaves when run
> with twistd -noy script.tac but directly from a standard python
> module? Lets assume I don't have access to the tac command and want to
> emulate the behaveour.
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python




More information about the Twisted-Python mailing list