环境:win7 64位,python 2.7.3

安装:

http://twistedmatrix.com/Releases/Twisted/14.0/Twisted-14.0.0.win-amd64-py2.7.exe

https://pypi.python.org/packages/2.7/z/zope.interface/zope.interface-4.1.1.win-amd64-py2.7.exe#md5=c3e22b49f84adaf169ec0d52eded4c8d

视需要安装:http://softlayer-sng.dl.sourceforge.net/project/wxpython/wxPython/3.0.0.0/wxPython3.0-win64-3.0.0.0-py27.exe

helloworld:

simpleserv.py(服务器端):

  1. from twisted.internet import reactor, protocol
  2.  
  3. class Echo(protocol.Protocol):
  4. """This is just about the simplest possible protocol"""
  5.  
  6. def dataReceived(self, data):
  7. "As soon as any data is received, write it back."
  8. self.transport.write(data)
  9.  
  10. def main():
  11. """This runs the protocol on port 8000"""
  12. factory = protocol.ServerFactory()
  13. factory.protocol = Echo
  14. reactor.listenTCP(8000,factory)
  15. reactor.run()
  16.  
  17. # this only runs if the module was *not* imported
  18. if __name__ == '__main__':
  19. main()

  simpleclient.py(客户端)

  1. from twisted.internet import reactor, protocol
  2.  
  3. # a client protocol
  4.  
  5. class EchoClient(protocol.Protocol):
  6. """Once connected, send a message, then print the result."""
  7.  
  8. def connectionMade(self):
  9. self.transport.write("hello, world!")
  10.  
  11. def dataReceived(self, data):
  12. "As soon as any data is received, write it back."
  13. print "Server said:", data
  14. data2= raw_input()
  15. if data2!='EOM':
  16. self.transport.write(data2)
  17. else:
  18. self.transport.loseConnection()
  19.  
  20. def connectionLost(self, reason):
  21. print "connection lost"
  22.  
  23. class EchoFactory(protocol.ClientFactory):
  24. protocol = EchoClient
  25.  
  26. def clientConnectionFailed(self, connector, reason):
  27. print "Connection failed - goodbye!"
  28. reactor.stop()
  29.  
  30. def clientConnectionLost(self, connector, reason):
  31. print "Connection lost - goodbye!"
  32. reactor.stop()
  33.  
  34. # this connects the protocol to a server runing on port 8000
  35. def main():
  36. f = EchoFactory()
  37. reactor.connectTCP("localhost", 8000, f)
  38. reactor.run()
  39.  
  40. # this only runs if the module was *not* imported
  41. if __name__ == '__main__':
  42. main()

  

测试:

1.开启服务器端

2.开启客户端:

更多文档参考这里

python-twisted的更多相关文章

  1. Python Twisted、Reactor

    catalogue . Twisted理论基础 . 异步编程模式与Reactor . Twisted网络编程 . reactor进程管理编程 . Twisted并发连接 1. Twisted理论基础 ...

  2. 【转】Python Twisted介绍

    Python Twisted介绍 作者:Jessica McKellar 原文链接 Twisted是用Python实现的基于事件驱动的网络引擎框架.Twisted诞生于2000年初,在当时的网络游戏开 ...

  3. Python twisted article

    学习python twisted 的好文章 An Introduction to Asynchronous Programming and Twisted Reference: http://kron ...

  4. python twisted教程[资料]

    python twisted教程 一,异步编程 http://www.douban.com/note/232200511/   python twisted教程 二:缓慢的诗 http://www.d ...

  5. Python - twisted web 入门学习之一

    原文地址:http://zhouzhk.iteye.com/blog/765884 python的twisted框架中带了一个web server: twisted web.现在看看怎么用. 一)准备 ...

  6. python twisted启动定时服务

    以下是python脚本send_mms.py #############################################!/usr/bin/python# -*- coding: ut ...

  7. Python Twisted介绍

    原文链接:http://www.aosabook.org/en/twisted.html 作者:Jessica McKellar Twisted是用Python实现的基于事件驱动的网络引擎框架.Twi ...

  8. python Twisted安装报错

    系统 mac pro 错误信息: IOError: [Errno 63] File name too long: '/var/folders/72/byjy11cs0dj_z3rjtxnj_nn000 ...

  9. 用python.twisted.logfile每天记录日志,并用不记录stdout中的内容

    #导入的头 from twisted.python import logfrom twisted.python.logfile import * #开始记录,输入日志名和存放的路径,setStdout ...

  10. Python Twisted系列教程3:初步认识Twisted

    作者:dave@http://krondo.com/our-eye-beams-begin-to-twist/ 译者:杨晓伟(采用意译) 可以从这里从头开始阅读这个系列. 用twisted的方式实现前 ...

随机推荐

  1. Error: java.lang.UnsatisfiedLinkError: no ntvinv in java.library.path

    Error Message When compiling or executing a Java application that uses the ArcObjects Java API, the ...

  2. JQuery思维导图

  3. c++重载运算符注意

    c++重载运算符的时候加&或不加: 如果加了&表示引用,说明用的都是同一块内存.如果不加,那么用的就是一份拷贝,即不同的内存. 一般连续操作的时候要加&. 可以重新定义一个对象 ...

  4. 缓存插件 Spring支持EHCache缓存

    Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能.其中,spring对EHCache提供了很好的支持. 在介绍Spring的缓 ...

  5. JSP九个内置对象

    JSP内置对象有: 1.request对象      客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 2.r ...

  6. jquery indexOf使用方法

    当无法确定在某个字符串中是否确实存在一个字符的时候,就可调用 indexOf() 和 lastIndexOf() 方法 indexOf() 和 lastIndexOf() 是js的用法,与jquery ...

  7. tarjan算法--求无向图的割点和桥

    一.基本概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 2.割点:无向连通图中 ...

  8. 【poj1001】 Exponentiation

    http://poj.org/problem?id=1001 (题目链接) 题意 求实数R的n次方,要求高精度. Solution SB题Wa了一下午,直接蒯题解. 高精度,小数点以及去前导后导零很麻 ...

  9. c++内存分配(new和delete)

    c中malloc和free是函数,包含在stdlib.h头文件中,分配成功返回指针,失败返回空指针. 与new的区别是: 1,malloc与free是C++/C语言的标准库函数,new/delete是 ...

  10. php中静态变量和静态方法

    1,静态变量:所有对象共享的变量成为静态变量.静态变量类似于全局变量,不过全局变量破坏对象的封装性,因此其对应于面向过程:静态变量对应于面向对象. 2,全局变量,全局变量的使用实例如下,声明全局变量时 ...