扭曲线接收不到被称为 [英] Twisted lineReceived not getting called
问题描述
我在python中构建命令行界面时遇到了一个奇怪的行为.这是可以重现此问题的代码的简化版本.
I encountered a strange behavior when i was building a command line interface in python. Here is the striped down version of the code that can reproduce the issue.
from twisted.internet import reactor, stdio
from twisted.protocols import basic
class CommandLine(basic.LineReceiver):
def __init__(self):
self.linebuf = ''
self.setLineMode()
# why lineReceived doesn't work?
# def lineReceived(self, data):
def dataReceived(self, data):
print 'data received ' + ' '.join([str(ord(c)) for c in data ])
print data
if __name__=='__main__':
stdio.StandardIO(CommandLine())
reactor.run()
上面的代码按预期工作,每次输入一行时,都会以接收到的数据108 115 115 10"的形式输出.这是使用dataReceived的示例输出:
The code above works as intended, out put in the form of "data received 108 115 115 10" is printed everytime a line is entered. Here is a sample output using dataReceived:
$ python cmdline.py
hello
data received 104 101 108 108 111 10
hello
^[[A
data received 27 91 65 10
但是,当我使用lineReceived而不是上面的代码中的dataReceived时,除了命令行本身的回显之外,什么都没有打印出来.使用lineReceived的示例输出:
However nothing gets printed out except the echo of the command line itself when I use lineReceived instead of dataReceived in the code above. Example output using lineReceived:
$ python cmdline.py
hello
^[[A
根据 lineReceived ,当LineReceiver以行模式接收到一行时,将调用lineReceived函数.
According the the documentation on lineReceived, the lineReceived function gets invoked when a line is received with the LineReceiver in line mode.
目前,我正在使用dataReceived使其工作.但是我想找出为什么lineReceived不能按预期工作.任何提示,建议和建议,将不胜感激!
For now I am using dataReceived to make it work. But I would like to find out why lineReceived is not working as intended. Any hint, suggestions, advice would be very much appreciated!
致谢.
推荐答案
原因是定界符常量,默认情况下设置为r'\ r \ n'(MS Windows定界符).尝试将其设置为"\ n"(Linux和Mac OS):
The reason is line delimiter constant which is set to r'\r\n' by default (MS Windows delimiter). Try to set it to '\n' (Linux and Mac OS) instead:
class CommandLine(basic.LineReceiver):
delimiter = '\n'
这篇关于扭曲线接收不到被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!