扭曲服务器,数控客户端
我将用一个小例子来演示我面临的问题。
class TestProtocol(basic.LineReceiver):
def lineReceived(self, line):
print line
只要我使用 telnet 客户端连接到服务器,一切都正常。但是,线路未接收连接并使用netcat发送数据。我有一种感觉,这与扭曲中的默认分隔符“\r\n”有关。
我如何制作一个服务器,以便客户端(telnet 和 nc)在连接到客户端时以相似的方式运行?
Ill demonstrate the problem I am facing with a small example.
class TestProtocol(basic.LineReceiver):
def lineReceived(self, line):
print line
Everything works fine as long as I use the telnet client to connect to the server. However, the line is not received connect and send the data using netcat. I have a feeling that this has something to do with the default delimiter being "\r\n" in twisted.
How could I make a server such that both the clients(telnet and nc) would behave in a similar manner when connecting to the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LineReceiver
仅支持一个分隔符。您可以指定它,但一次只能有一个。一般来说,如果您想支持多个分隔符,您需要实现一个支持它的新协议。您可以查看 LineReceiver 的实现,了解有关如何实现基于线路的协议的一些想法。netcat 发送您输入的任何内容,因此分隔符通常是 \n (但它可能因平台和终端仿真器而异)。对于 \n 的特殊情况(它是默认
LineReceiver
分隔符\r\n
的子字符串),您还可以使用另一个技巧。将TestProtocol.delimiter
设置为"\n"
,然后从传递给的行末尾去除
(如果有)。"\r"
lineReceivedLineReceiver
only supports one delimiter. You can specify it, but there can only be one at a time. In general, if you want to support multiple delimiters, you'll need to implement a new protocol that supports that. You could take a look at the implementation of LineReceiver for some ideas about how a line-based protocol is implemented.netcat sends whatever you type, so the delimiter is often \n (but it may vary from platform to platform and terminal emulator to terminal emulator). For the special case of \n, which is a substring of the default
LineReceiver
delimiter\r\n
, there's another trick you can use. Set theTestProtocol.delimiter
to"\n"
and then strip the"\r"
off the end of the line passed tolineReceived
if there is one.另一种解决方法是将
nc
与-C
开关结合使用。来自手册:
或按照 @CraigMcQueen 建议:
带有
-c
开关的套接字 (Ubuntu 软件包)。Another workaround is to use
nc
with the-C
switch.From the manual:
or as @CraigMcQueen suggested:
socket with
-c
switch (Ubuntu package).Twisted 的
LineReceiver
和LineOnlyReceiver
仅支持一个行结束分隔符。以下是
UniversalLineReceiver
和UniversalLineOnlyReceiver
的代码,它们重写了dataReceived()
方法,支持通用行结尾(CR+LF 的任意组合, CR 或 LF)。使用正则表达式对象delimiter_re
检测换行符。请注意,它们用比我想要的更多的代码覆盖函数,因此如果底层 Twisted 实现发生更改,它们可能会中断。我已经测试过它们可以与 Twisted 13.2.0 配合使用。本质的变化是使用
delimiter_re
。split()
来自re
模块。Twisted's
LineReceiver
andLineOnlyReceiver
only support one line ending delimiter.Here is code for
UniversalLineReceiver
andUniversalLineOnlyReceiver
, which override thedataReceived()
method with support for universal line endings (any combination of CR+LF, CR or LF). The line breaks are detected with the regular expression objectdelimiter_re
.Note, they override functions with more code in them than I'd like, so there's a chance that they may break if the underlying Twisted implementation changes. I've tested they work with Twisted 13.2.0. The essential change is the use of
delimiter_re
.split()
from there
module.