python手记-twisted(2)
2016-07-07 17:51
295 查看
关闭连接
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.
^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()
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.csdn.net/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()
相关文章推荐
- 标签传播算法(LPA)Python实现
- dbus 和 policykit 实例篇(python)
- Python中的os.path路径模块中的操作方法总结
- python 2.x将raw_input()方式输入的字符串转换为unicode编码
- 【编程语言】Python 函数多值返回
- Python学习笔记2:函数式编程
- **PYTHON** 初始python
- python 读写文件 个人 常犯错误 注意事项
- python机器学习环境部署笔记
- Python 语言及其应用 Chapter_7_Note 1 正则表达匹配
- Python_基础_01
- 简介Python的collections模块中defaultdict类型的用法
- 利用Python gensim基于中文语料建立LSA隐性语义模型
- Python Standard Library——JSON例子
- Python3.3 print函数用法,print 格式化输出
- 什么样的类型可以作为python字典的key
- Python学习 对函数fact(n)编写doctest并执行
- python pycurl模块
- Python的collections模块中的OrderedDict有序字典
- python3基础