gpt4 book ai didi

python - 我扭曲的服务器有什么问题,它应该采用 .exe 并将其 stdio 发送给任何询问的人。相反,它不发送任何东西

转载 作者:太空狗 更新时间:2023-10-30 01:35:19 25 4
gpt4 key购买 nike

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()

有问题的程序首先打印东西。当我通过原始套接字连接到它时,它不会发送任何内容。这是输出:

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

我错过了什么吗?

提前致谢。

最佳答案

reactor.spawnProcess(proto, addr) 替换为 reactor.spawnProcess(proto, addr, ['maze'], {})

过去的经验表明,如果您不将 exe 名称作为第一个参数传递,则不会发生任何有用的事情。然而,我还没有找到一个合理的解释来解释为什么会发生这种情况。

你也不需要global reactor。当您导入 reactor 时,您将其添加到顶级脚本命名空间。这意味着同一文件中的所有函数和类都可以使用它而无需声明全局或再次导入。

另外,你不应该使用 sleep(0.01) 因为:

  1. 它不是内置函数。您需要从 time 导入它 module .
  2. Twisted 是一个异步框架,其中函数应不惜一切代价避免阻塞,而 time.sleep() ( link ) 根据其定义是阻塞调用。

您应该改为使用 reactor.callLater() link您提供的地方将是一个回调和一个时间段。这将使 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()

关于python - 我扭曲的服务器有什么问题,它应该采用 .exe 并将其 stdio 发送给任何询问的人。相反,它不发送任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5838274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com