Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you you run a Twisted application via Python (instead of via Twisted)?

Tags:

I am working my way through learning Twisted, and have stumbled across something I'm not sure I'm terribly fond of - the "Twisted Command Prompt". I am fiddling around with Twisted on my Windows machine, and tried running the "Chat" example:

from twisted.protocols import basic

class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print "Got new client!"
        self.factory.clients.append(self)

    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        print "received", repr(line)
        for c in self.factory.clients:
            c.message(line)

    def message(self, message):
        self.transport.write(message + '\n')


from twisted.internet import protocol
from twisted.application import service, internet

factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []

application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)

However, to run this application as a Twisted server, I have to run it via the "Twisted Command Prompt", with the command:

twistd -y chatserver.py

Is there any way to change the code (set Twisted configuration settings, etc) so that I can simply run it via:

python chatserver.py

I've Googled, but the search terms seem to be too vague to return any meaningful responses.

Thanks.

like image 652
Mike Trpcic Avatar asked Dec 13 '09 22:12

Mike Trpcic


People also ask

How do you run a twist?

You could run twistd from a normal command prompt if you set your %PATH% properly or invoke it with the full path. (From How do you you run a Twisted application via Python (instead of via Twisted)?) Where in C:\path\to\twistd.py you insert the path to the twistd.py file.

What is the use of twisted in Python?

Twisted is an open source asynchronous event-driven engine for network application development written entirely in Python and distributed under the MIT license. It allows you to create a SMTP, HTTP, proxy and ssh servers in a matter of minutes without the use of traditional threading models.

What is twisted package Python?

Twisted is an open source network framework written entirely in Python. It allows you to create a SMTP, HTTP, proxy and ssh servers (and more) in Python with minimal effort.

What is twisted framework?

Twisted is an event-based framework for internet applications, supporting Python 3.6+. It includes modules for many different purposes, including the following: twisted. web: HTTP clients and servers, HTML templating, and a WSGI server. twisted.


2 Answers

I don't know if it's the best way to do this but what I do is instead of:

application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)

you can do:

from twisted.internet import reactor
reactor.listenTCP(1025, factory)
reactor.run()

Sumarized if you want to have the two options (twistd and python):

if __name__ == '__main__':
    from twisted.internet import reactor
    reactor.listenTCP(1025, factory)
    reactor.run()
else:
    application = service.Application("chatserver")
    internet.TCPServer(1025, factory).setServiceParent(application)

Hope it helps!

like image 116
chaos.ct Avatar answered Sep 24 '22 04:09

chaos.ct


Don't confuse "Twisted" with "twistd". When you use "twistd", you are running the program with Python. "twistd" is a Python program that, among other things, can load an application from a .tac file (as you're doing here).

The "Twisted Command Prompt" is a Twisted installer-provided convenience to help out people on Windows. All it is doing is setting %PATH% to include the directory containing the "twistd" program. You could run twistd from a normal command prompt if you set your %PATH% properly or invoke it with the full path.

If you're not satisfied with this, perhaps you can expand your question to include a description of the problems you're having when using "twistd".

like image 30
Jean-Paul Calderone Avatar answered Sep 23 '22 04:09

Jean-Paul Calderone