python手记-twisted(2)

本文介绍如何使用Xshell进行远程连接操作,并展示了一个简单的Telnet客户端交互过程。此外,通过Python Twisted框架实现了一个简易的回显服务器,该服务器能够接收客户端消息并反馈,遇到特定命令则关闭连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关闭连接

Xshell 5 (Build 0655)
Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved.


Type `help' to learn how to use Xshell prompt.
[c:\~]$ telnet 120.55.*.* 8001

Connecting to 120.55.*.*:8001...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
hello
hello
world
world
quit
Connection closed by foreign host.


Disconnected from remote host(120.55.69.31:8001) at 17:45:33.


Type `help' to learn how to use Xshell prompt.

本博客所有内容是原创,如果转载请注明来源

http://blog.youkuaiyun.com/myhaspl/


如果输入quit则关闭连接,通过下面语句

self.transport.loseConnection()

protocol不保存永久数据,每个连接初始化生成protocol实例,连接关闭时退出。而永久数据由factory负责

^C[myhaspl@iZ23mdqdp94Z ~]$ cat  learn/l1/myserver.pl
from twisted.internet.protocol import Protocol
from twisted.internet import reactor
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint

class Echo(Protocol):
    
     def dataReceived(self,data):
         if data!="quit":
              self.transport.write(data)
         else:
              self.transport.loseConnection()

class EchoFactory(Factory):
     def buildProtocol(self,addr):
          return Echo()    

endpoint=TCP4ServerEndpoint(reactor,8001)
endpoint.listen(EchoFactory())
reactor.run()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值