Gly*_*can 5 python windows-xp twisted
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's '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()
Run Code Online (Sandbox Code Playgroud)
有问题的程序首先打印东西.当我连接到它时,通过原始套接字,它不会发送任何东西.这是输出:
Preall test works!
Imports done
About to do stuff
Runing (connect)
Connected
Prgproto instance made
Procces spawned!
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
提前致谢.
reactor.spawnProcess(proto, addr)用。。。来代替reactor.spawnProcess(proto, addr, ['maze'], {})。
过去的经验表明,如果您不将 exe 名称作为第一个参数传递,那么不会发生任何有用的事情。但我还没有找到合理的解释来解释为什么会发生这种情况。
而且你也不需要global reactor。当您导入时,reactor您将其添加到顶级脚本命名空间。这意味着同一文件中的所有函数和类都可以使用它,而无需声明全局或再次导入。
另外,您不应该使用,sleep(0.01)因为:
您应该使用提供回调和时间段的reactor.callLater() 链接。这将使twisted 在您等待时处理其他事情(例如新连接)。
最后,您现在的代码将要求用户在程序提出任何要求之前输入内容。这是因为getWrit只发送缓冲区中已经存在的内容,而不是询问用户。这意味着如果用户在调用之前没有发送任何数据,getWrit那么它将仅返回一个空字符串。
如果你使用 deferred 会是一个更好的主意。然后你要做的就是调用它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's '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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
414 次 |
| 最近记录: |