[Twisted-Python] Client Application service.Service

Enrique Samson Jr. enriquejr at gmail.com
Wed Mar 16 20:21:44 MDT 2016


Hi Tim,

I think you need to keep a reference to an instance of your
MyClientProtocol. Your example does not show how you actually connected
your bot to a server but I assume it follows on the call to load_config on
startService. One way to do this:

class MyService(Service):
  def __init__(self):
    self._client = None

  def clientConnected(self, prot):
    # set reference to the protocol instance
    self._client = prot

  def startService():
    self.config = load_config(config_file)

    # connect bot to the server
    f = MyClientFactory(self)
    reactor.connectTCP(self.config.host, self.config.port, f)

  def sendMessage(self, msg):
    if self._client:
      self._client.sendLine(msg)

Client reference can be set from the protocol instance when connection has
been made:

class MyClientProtocol(protocol.Protocol):
  #
  def connectionMade(self):
     self.factory.service.clientConnected(self)

HTH,
Enrique

On Thu, Mar 17, 2016 at 8:24 AM, Tim Hughes <thughes at thegoldfish.org> wrote:

> Woops : accidentally sent too early
>
> Hi all,
>
> I am looking into twisted for writing a client application (bot style) and
> most of the examples I find are for server style applications.
>
> Looking at the finger example and
> https://github.com/jdavisp3/twisted-intro/blob/master/twisted-server-3/fastpoetry.py
> I am just wondering where is best to put my application logic.
>
> The protocol is basically a ascii line style application
>
> It feels to me that my Service should be the main business logic  and the
> protocol should just be a simple send/receive something like this gist
>
>
> https://gist.github.com/timhughes/f85da0c2d88ecb4ab5e3#file-twisted_application_client-py-L36
>
> I was then planning on writing web interface that interacted with the
> service to send messages and display the response.
>
> My main issue here is how to send the message from the service via the
> protocol. I cannot find any good examples of this and am beginning to think
> I am conceptualising incorrectly or over thinking it. If anyone can point
> me to some examples or point me in the right direction it would be awesome.
> Hopefully my example makes sense.
>
> Cheers
>
> Tim
>
> Tim Hughes
> mailto:thughes at thegoldfish.org
>
> Hi all,
>
> I am looking into twisted for writing a client application (bot style) and
> most of the examples I find are for server style applications.
>
>
>
> Looking at the finger example and
> https://github.com/jdavisp3/twisted-intro/blob/master/twisted-server-3/fastpoetry.py
> I am just wondering where is best to put my application logic.
>
>
> The protocol is basically a ascii line style application
>
>
> It feels to me that my Service should be the core and the protocol should
> just be something like
>
> def send_mesage(message):
>
>
>
>
>
>
>
>
>
>
> Tim Hughes
> mailto:thughes at thegoldfish.org
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20160317/e253e5db/attachment-0002.html>


More information about the Twisted-Python mailing list