python - What's wrong with my twisted server that's supposed to take a .exe and send its stdio to anyone who asks. Instead, it doesn't send anything -
print 'preall test works!' twisted.internet import reactor, protocol 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. send on transport, if it's 'i want input', need 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 prg' data = self.data self.data = '' return data def connectionmade(self): global reactor print 'connected' proto = self.prgproto(self) addr = "c:\\documents , 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 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 stuff first thing. when connect it, via raw sockets, doesn't send anything. here's output:
preall test works! imports done stuff runing (connect) connected prgproto instance made procces spawned!
am missing anything?
thanks in advance.
replace reactor.spawnprocess(proto, addr)
reactor.spawnprocess(proto, addr, ['maze'], {})
.
past experience has show if don't pass exe name first argument nothing useful happens. have yet find reasonable explanation why happens.
also don't need global reactor
. when import reactor
add top level script namespace. means functions , class in same file can use without declaring global or importing again.
also, should not using sleep(0.01)
because:
- its not builtin function. need import
time
module. - twisted asynchronous framework function should avoid blocking @ costs, ,
time.sleep()
(link) definition blocking call.
you should instead use reactor.calllater()
link provide callback , time period. let twisted handle other things (like new connection) while wait.
finally, code @ moment require user enter input before program asks any. because getwrit
sends stuff in buffer rather asking user. means if user hasn't sent data before getwrit
called return empty string.
it better idea if used deferred. call getwrit
immanently return deferred , clear data buffer. in datareceived
append data buffer until got newline character (\n
). @ point call deferred set in getwrit
.
something this:
print 'preall test works!' twisted.internet import reactor, protocol, defer 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. send on transport, if it's 'i want input', need activate input.""" print 'sub said: '+data if data == "input": print 'sub wants input' self.transportout.write("input") d = self.out.getwrit() # getwrit returns deferred. store in d make code more readable d.addcallback(self.sendinput) # here add self.sendinput callback chain. # way self.sendinput gets called user input. else: self.transportout.write(data) def sendinput(self, data): self.transport.write(data) def getwrit(self): print 'proto gave input prg' self.deferred = defer.deferred() self.data = '' return self.deferred def connectionmade(self): print 'connected' proto = self.prgproto(self) addr = "c:\\documents , 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: # got newline character, , there deferred call, lets call d, self.deferred = self.deferred, none # set self.deferred none stop mistakes later d.callback(self.data) # call deferred data. send data sendinput above. self.data = '' # clear buffer print 'about 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()
Comments
Post a Comment