Created
November 1, 2014 22:51
-
-
Save mmattice/c0bf79a727b8a91c3b90 to your computer and use it in GitHub Desktop.
example twistd service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.protocols.amp import AMP | |
from twisted.internet import reactor | |
from twisted.internet.protocol import Factory | |
from twisted.internet.endpoints import TCP4ServerEndpoint | |
from twisted.application.service import Application | |
from twisted.application.internet import StreamServerEndpointService | |
class Count(amp.Command): | |
arguments = [('n', amp.Integer())] | |
response = [('ok', amp.Boolean())] | |
class Counter(amp.AMP): | |
@Count.responder | |
def count(self, n): | |
print 'received:', n | |
n += 1 | |
if n < 10: | |
print 'sending:', n | |
self.callRemote(Count, n=n) | |
return {'ok': True} | |
# twistd -n -y count_server.py | |
application = Application('test AMP server') | |
endpoint = TCP4ServerEndpoint(reactor, 8750) | |
factory = Factory() | |
factory.protocol = Counter | |
service = StreamServerEndpointService(endpoint, factory) | |
service.setServiceParent(application) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment