Author Topic: Python + Torque  (Read 879 times)

How do I connect a python script and torque through HTTP or TCP objects?

Yes.
When I get home I'll post some code


So, many people will suggest different things for different things.
First different things being the library, second diffrent things being what you want to do with it.
Flask - HTTP Servers, Websites
Twisted - General TCP servers
SocketServer - General TCP Servers
sockets - General TCP

For this Demonstration I'll be using my TTS mod.
$TTS::Port = 1401;
function TTSTCP::onConnected(%this)
{
   %this.isConnected = true;
   if(%this.sendOnConnect !$= "")
      %this.send(%this.sendonconnect @ "\r\n");
}
function TTSTCP::onDisconnect(%this)
{
   %this.isConnected = false;
}

function TTSTCP::onConnectFailed(%this)
{
   %this.isConnected = false;
}

function sayText(%str)
{
   if(!isObject(TTSTCP))
   {
      new TCPObject(TTSTCP);
      TTSTCP.sendOnConnect = %str;
      TTSTCP.connect("localhost:" @ $TTS::Port);
   }
   else
   {
      if(!TTSTCP.isConnected)
      {
         TTSTCP.sendOnConnect = %str;
         TTSTCP.connect("localhost:" @ $TTS::Port);
      }
      else
      {
         TTSTCP.send(%str @ "\r\n");
      }
   }
}


let me break it down for you.
TCPObject::onConnected(%this) is called when a TCPObject is successfully connected to a server. In this case I just set a variable to true and sent an autosend if there was one.
TCPObject::onDisconnect(%this) is called when a TCPObject is disconnected from a server. In this case I just set it to turn off the variable that onConnected set to true.
TCPObject::onConnectFailed(%this) is called when a TCPObject didn't successfully connect to the server.

So, when connecting to a TCP Server, you use the TCPObject::connect(%this,%ip) command.
new TCPObject(TTSTCP);
TTSTCP.sendOnConnect = %str;
TTSTCP.connect("localhost:" @ $TTS::Port);

Ignore the second line, the first line initiates the TCP Object, named TTSTCP (TextToSpeech TCP). The third line calls .connect(%ip); which basically connects to the IP put in. (the IP must include the port).

Sending to a TCP is pretty easy. You just use the TCPObject::send(%this,%msg) command. For example
TTSTCP.send(%str @ "\r\n"); TTSTCP is the TCPObject, of course, and %str is the message to be sent. At the end you put \r\n. If you don't, it wont sent until you do put it there, because the way the TCP Server works is because it grabs lines from the connection. And you can keep adding lines to the connection but if you don't end the line it wont do anything.
If you want to recieve from the TCP Server. You would create a TTSTCP::onLine(%this,%line) function for the name of your TCPObject. the %line is a single line from the output. If you want tons of lines I suggest doing something where you keep on stacking up on the lines onto a variable and then when disconnected or received a certain message, do something.

now onto the fun stuff, the TCP Server. Ill be modifying the original source to just debug (print) the messages recieved instead of outputing to voice.
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class MyProtocol(LineReceiver):
   def __init__(self):
      print "My Protocol has been created!"
   def lineReceived(self, line):
      print "Line recieved: " + line
class MyFactory(Factory):
   protocol = MyProtocol

reactor.listenTCP(1401, MyFactory())
reactor.run()


It's a simple idea. the MyProtocol handles all of the IO stuff. I don't remember what Factory is used for other than having a protocol. So the __init__ function is called when the MyProtocol Object is created. Then you can do what ever, have any
var declarations here. lineRecieved(self,line) is the function called when a singe lineis received. To send a message to the whom ever use .write(msg + '\n') and it should send. I dont use Twisted that much so I forget the command to send.

That should be enough for now, to get you started. I may have made a typo/error here, post if i did.

I don't remember what Factory is used for other than having a protocol.

In Twisted terms, the factory (name from the fact that it produces protocol instances) represents the server, storing things like global data, keeping track of connections, etc. Protocols represent individual connected sockets (clients).

In Twisted terms, the factory (name from the fact that it produces protocol instances) represents the server, storing things like global data, keeping track of connections, etc. Protocols represent individual connected sockets (clients).
Oh I understand now.

Thanks for the help Brian!
It worked :D
« Last Edit: October 13, 2012, 08:26:11 PM by Aide33 »