python手记-twisted(4)

from twisted.internet.protocol import Protocol
from twisted.internet import reactor
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
#http://blog.csdn.net/myhaspl
class Echo(Protocol):

     def connectionMade(self):
         self.factory.numProtocols=self.factory.numProtocols+1
         self.transport.write("welcome!\nthere are currently %d open connections.\n"%(self.factory.numProtocols,))

     def dataReceived(self,data):
         mydata=data.strip()
	 if mydata!="quit" and  mydata!="quit\r\n" and mydata!="quit\r":
              self.transport.write(self.factory.message+data)
         else:
              self.transport.write(self.factory.message+"byebye\n")
              self.transport.numProtocols=self.factory.numProtocols-1
              self.transport.loseConnection()
     def connectionLost(self, reason):
         self.factory.numProtocols = self.factory.numProtocols - 1

#http://blog.csdn.net/myhaspl
class EchoFactory(Factory):
     #use the default buildProtocol to create protocol
     protocol=Echo
     numProtocols=0

     def __init__(self,message=None):
          self.message=message or "hello,world"
 #http://blog.csdn.net/myhaspl
endpoint=TCP4ServerEndpoint(reactor,8001)
endpoint.listen(EchoFactory("myhaspl:"))
reactor.run()

未经博主允许不得转载。http://blog.csdn.net/myhaspl

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

http://blog.csdn.net/myhaspl/

$ telnet 120.55.*.* 8001

Trying 120.55.*.* ...

Connected to 120.55.*.* .

Escape character is ‘^]‘.

welcome!

there are currently 1 open connections.

hello

myhaspl:hello

world

myhaspl:world

quit

myhaspl:byebye

Connection closed by foreign host.

时间: 2024-08-05 20:16:02

python手记-twisted(4)的相关文章

python手记-twisted(2)

关闭连接 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 she

Python 手记

Python 手记 python 2.7 notepid ++ 编辑器 <简明 Python 教程> www.byteofpython.info 获得人生中的成功需要的专注与坚持不懈多过天才与机会. ——C.W. Wendte wxPython.Twisted和Python图像库 Python.org/download 点击控制面板->系统->高级->环境变量.在“系统变量”表单中点击叫做PATH的变量,然后编辑这个变量,把;C:\Python23加到它的结尾.当然,是Pyt

python之twisted模块安装

Twisted是一个事件驱动的网络框架. 最近开始学习了解Twisted,首先肯定要安装twisted模块. 但是在cmd下执行:pip install twisted 出现了下面的问题:"error:Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat) 我电脑是pyhton3.4环境(python2.7也有),没有装VC++ 10.0,所以根据error提示,我得去装个VC++ 10.0?? 但是我并不想~,于

为什么 Node.js 这么火,而同样异步模式 Python 框架 Twisted 却十几年一直不温不火?

twisted是一个强大的异步网络框架,应用的面也非常广,但是没有这几年才出现的Node.js火,社区.文档也是很少可怜我觉得二者其实在本质上差不多,而且python使用起来还是比较容易一些的 匿名用户 因为,它给了一大部分程序猿幻觉比如前后端统一,脚本也能性能很屌,做Demo搜搜快什么的,但实际上,这仅仅是幻觉罢了…… 正是因为这样的幻觉是“看得到”的,又有一个响当当的干爹Google,因此Node的曝光率远高于后端常规语言就不足为奇了. 论速度,你一个带JIT的跟常规脚本语言的虚拟机比,没到

Python 安装Twisted 提示python version 2.7 required,which was not found in the registry

由于我安装Python64位的,下载后没注册,安装Twisted时老提示“python version 2.7 required,which was not found in the registry”错误 解决方法 1.任意位置存放reg.py文件 # # script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry setting

Python手记

字符串的拼接 1."+",如果是字符和数字相连,要使用str()函数对于数字进行字符转化: 2.join() 3.",",链接的两个字符串之间用空格做关联 4.占位符 tmp +=1 #print 'r'%tmp print("row num is: %s"%(tmp)) print('value is: %s; RowNum is: %s'%(cellValue,rowNo)) #多个占位 python的全局变量 第一种是def consts(

菜鸟教程 Python 手记 1

Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. 编码 编码默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -*- 多行语句 Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句,例如: total = item_one + item_two + item_three 在 [], {}, 或 () 中

Python 手记-3

1.PyCharm配置模板 file------settings-----editer-----filie*--------templates------python script 添加 #!/usr/bin/env python # -*- coding:utf-8 -*- #Writed by liyang 这样,创建同样文件的时候,会自动添加这些内容到新建文件 2.小练习:生成字典 文件内容: alex|123|1 eric|123|1 emmy|123|1 obj = file('log

python手记(7)------字典(操作方法)

1.dict方法概述 In[70]: dir(dict) Out[69]: ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ite