[Twisted-Python] alternative to tac
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.
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@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Thanks. I'll try it out. On Tue, Oct 7, 2008 at 11:18 AM, Werner Thie <wthie@thiengineering.ch> wrote:
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@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Oct 7, 2008, at 5:18 AM, Werner Thie wrote:
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.
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.
[snip]
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()
[snip] I don't think this really emulates twistd's behavior very well. twistd does roughly one trillion other things during startup that won't happen here. Maybe you don't need any of this stuff, but I'll add my two cents about how I've dealt with this, which is to use twistd plugins, and call twistd's script functions directly: from twisted.scripts import twistd config = twistd.ServerOptions() args = ['--pidfile', 'mytwistd.pid', '-l', 'mytwistd.log', 'myplugin'] config.parseOptions(args) twistd.runApp(config) This lets me easily specify configuration details, but should be reasonably forward-compatible. Also, I can keep all my code inside a package, and as long as it's in the system path, my plugin will be available to twistd. There was some talk ages ago about making twistd a little more developer-friendly (so that you could more easily pick and choose the parts of it you wanted to use) but I don't remember the details, or if any progress was made. -phil
participants (3)
-
Phil Christensen
-
Vasil Vangelovski
-
Werner Thie