What is wrong with my twisted server, which should take .exe and send it stdio to anyone who asks. Instead, he sends nothing

print 'Preall test works!'
from twisted.internet import reactor, protocol
from twisted.python import log
import sys
print 'Imports done'

class PrgShell(protocol.Protocol):
    data = ''
    class PrgProto(protocol.ProcessProtocol):
        def __init__(self, out):
            print 'Prgproto instance made'
            self.transportout = out.transport
            self.out = out
        def outReceived(self, data):
            """Called when process sends data. We send it on to transport, however if it 'I want input', we need to activate input."""
            print 'Sub said: '+data
            if data == "input":
                print 'Sub wants input'
                self.transportout.write("input")
                sleep(0.01)
                self.transport(self.out.getWrit())
            else:
                self.transportout.write(data)



    def getWrit(self):
        print 'Proto gave input to prg'
        data = self.data
        self.data = ''
        return data 

    def connectionMade(self):
        global reactor
        print 'Connected'
        proto = self.PrgProto(self)
        addr = "C:\\Documents and Settings\\papa\\My Documents\\Python\\Files\\Maze\\exe\\maze.exe"
        reactor.spawnProcess(proto, addr)
        print 'Procces spawned!'


    def dataReceived(self, data):
        print 'Data recived: '+data
        self.data+=data

print 'About to do stuff'
factory = protocol.ServerFactory()
factory.protocol = PrgShell
#f = open("errors.txt", 'w')
#log.startLogging(f)
#print 'Logging started'
reactor.listenTCP(8000,factory)
print 'Runing'
reactor.run()

The program in question prints things first. When I connect to it through raw sockets, it does not send anything. Here's the conclusion:

Preall test works!
Imports done
About to do stuff
Runing (connect)
Connected
Prgproto instance made
Procces spawned!

Did I miss something?

Thanks in advance.

+5
source share
1 answer

Replace reactor.spawnProcess(proto, addr)with reactor.spawnProcess(proto, addr, ['maze'], {}).

Past experience shows that if you do not pass the name exe as the first argument, nothing useful will happen. However, I have yet to find a reasonable explanation of why this is happening.

global reactor. reactor, script. , .

, sleep(0.01), :

  • . time .
  • Twisted - , , time.sleep() () .

reactor.callLater() , , . (, ), .

, , , . , getWrit , . , - getWrit, .

, . getWrit, . dataReceived , (\n). getWrit.

- :

print 'Preall test works!'
from twisted.internet import reactor, protocol, defer
from twisted.python import log
import sys
print 'Imports done'

class PrgShell(protocol.Protocol):
    data = ''
    class PrgProto(protocol.ProcessProtocol):
        def __init__(self, out):
            print 'Prgproto instance made'
            self.transportout = out.transport
            self.out = out
        def outReceived(self, data):
            """Called when process sends data. We send it on to transport, however if it 'I want input', we need to activate input."""
            print 'Sub said: '+data
            if data == "input":
                print 'Sub wants input'
                self.transportout.write("input")
                d = self.out.getWrit() # getWrit returns a deferred. We store it in d to make the code more readable
                d.addCallback(self.sendInput) # Here we add self.sendInput to the callback chain.
                                              # This way self.sendInput gets called with the user input.
            else:
                self.transportout.write(data)

        def sendInput(self, data):
            self.transport.write(data)


    def getWrit(self):
        print 'Proto gave input to prg'
        self.deferred = defer.deferred()
        self.data = ''
        return self.deferred

    def connectionMade(self):
        print 'Connected'
        proto = self.PrgProto(self)
        addr = "C:\\Documents and Settings\\papa\\My Documents\\Python\\Files\\Maze\\exe\\maze.exe"
        reactor.spawnProcess(proto, addr, ['maze'], {})
        print 'Procces spawned!'


    def dataReceived(self, data):
        print 'Data recived: '+data
        self.data+=data

        if self.data.endswith('\n'):
            if self.deferred:
                # We got a newline character, and there is a deferred to call, so lets call it
                d, self.deferred = self.deferred, None # This will set self.deferred to none to stop mistakes later

                d.callback(self.data) # Call the deferred with data. This will send the data to sendInput above.

                self.data = '' # Clear the buffer

print 'About to do stuff'
factory = protocol.ServerFactory()
factory.protocol = PrgShell
#f = open("errors.txt", 'w')
#log.startLogging(f)
#print 'Logging started'
reactor.listenTCP(8000,factory)
print 'Runing'
reactor.run()
+1

All Articles