<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users2131</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/2131" rel="self"/>
  <title>https://www.google.com/accounts/o8/id?id=AItOawkapZWfDKPlzk45bKWi1klRG7GdRd4leuU</title>
  <updated>Thu Jul 01 07:21:04 -0700 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor517305</id>
    <published>2010-07-01T07:21:04-07:00</published>
    <title>[Python] On Twisted tcp client</title>
    <content type="html">&lt;p&gt;Line ending is wrong. Shame on me. '\r\n' works just fine!
&lt;br /&gt;And I should stop the reactor!&lt;/p&gt;

&lt;pre&gt;#!/usr/bin/env python                                                                                                                                             

from twisted.internet.protocol import ClientCreator, Protocol
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import sys

class Sender(Protocol):
    def sendCommand(self, command):
        self.transport.write(command)

    def dataReceived(self, data):
        print &amp;quot;Received:&amp;quot;, data
        reactor.stop()

PORT = 5005
HOST = 'localhost'

def sendCommand(command):
    def test(d):
        d.sendCommand(command)
    c = ClientCreator(reactor, Sender)
    c.connectTCP(HOST, PORT).addCallback(test)

if __name__ == '__main__':
    if len(sys.argv) != 2 or sys.argv[1] not in ['stop', 'next_call', 'force']:
        sys.stderr.write('Usage: %s: {stop|next_call|force}\n' % sys.argv[0])
        sys.exit(1)
    sendCommand(sys.argv[1]+'\r\n')
    reactor.run()&lt;/pre&gt;</content>
    <author>
      <name>https://www.google.com/accounts/o8/id?id=AItOawkapZWfDKPlzk45bKWi1klRG7GdRd4leuU</name>
      <email>enricocarlesso@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1342-twisted-tcp-client/refactors/517305" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1342</id>
    <published>2010-07-01T07:15:47-07:00</published>
    <updated>2011-05-09T00:30:28-07:00</updated>
    <title>[Python] Twisted tcp client</title>
    <content type="html">&lt;p&gt;I've an application in twisted which, among other features, has a TCPServer for management. Telnetting to port 5005 you can give commands like stop and force. Now I need to write a simple command-line interface to it, but cannot have it working&lt;/p&gt;

&lt;pre&gt;#!/usr/bin/env python                                                                                                                                             

from twisted.internet.protocol import ClientCreator, Protocol
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import sys

class Sender(Protocol):
    def sendCommand(self, command):
        print &amp;quot;invio&amp;quot;, command
        self.transport.write(command)

    def dataReceived(self, data):
        print &amp;quot;DATA&amp;quot;, data

PORT = 5005
HOST = 'localhost'

def sendCommand(command):
    def test(d):
        print &amp;quot;Invio -&amp;gt;&amp;quot;, command
        d.sendCommand(command)
    c = ClientCreator(reactor, Sender)
    c.connectTCP(HOST, PORT).addCallback(test)

if __name__ == '__main__':
    if len(sys.argv) != 2 or sys.argv[1] not in ['stop', 'next_call', 'force']:
        sys.stderr.write('Usage: %s: {stop|next_call|force}\n' % sys.argv[0])
        sys.exit(1)
    sendCommand(sys.argv[1]+'\n')
    reactor.run()
&lt;/pre&gt;</content>
    <author>
      <name>https://www.google.com/accounts/o8/id?id=AItOawkapZWfDKPlzk45bKWi1klRG7GdRd4leuU</name>
      <email>enricocarlesso@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1342-twisted-tcp-client" rel="alternate"/>
  </entry>
</feed>

