环境: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(服务器端):

from twisted.internet import reactor, protocol

class Echo(protocol.Protocol):
"""This is just about the simplest possible protocol""" def dataReceived(self, data):
"As soon as any data is received, write it back."
self.transport.write(data) def main():
"""This runs the protocol on port 8000"""
factory = protocol.ServerFactory()
factory.protocol = Echo
reactor.listenTCP(8000,factory)
reactor.run() # this only runs if the module was *not* imported
if __name__ == '__main__':
main()

  simpleclient.py(客户端)

from twisted.internet import reactor, protocol

# a client protocol

class EchoClient(protocol.Protocol):
"""Once connected, send a message, then print the result.""" def connectionMade(self):
self.transport.write("hello, world!") def dataReceived(self, data):
"As soon as any data is received, write it back."
print "Server said:", data
data2= raw_input()
if data2!='EOM':
self.transport.write(data2)
else:
self.transport.loseConnection() def connectionLost(self, reason):
print "connection lost" class EchoFactory(protocol.ClientFactory):
protocol = EchoClient def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop() def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
reactor.stop() # this connects the protocol to a server runing on port 8000
def main():
f = EchoFactory()
reactor.connectTCP("localhost", 8000, f)
reactor.run() # this only runs if the module was *not* imported
if __name__ == '__main__':
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. 传智168期JavaEE就业班 day03-js

    * 课程回顾: * CSS * CSS的简介 * 层叠样式表. * CSS与HTML的结合(4种) * HTML的标签提供了属性 style="CSS的代码" * HTML提供了标 ...

  2. Qt模型/视图框架----简单的例子

    #include<qapplication.h> #include<qfilesystemmodel.h> #include<qtreeview.h> #inclu ...

  3. Java设计模式-桥接模式(Bridge)

    桥接模式就是把事物和其具体实现分开,使他们可以各自独立的变化.桥接的用意是:将抽象化与实现化解耦,使得二者可以独立变化,像我们常用的JDBC桥DriverManager一样,JDBC进行连接数据库的时 ...

  4. 【POJ 2250】Compromise(最长公共子序列LCS)

    题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...

  5. 【前端】Sublime text3 插件HTML/CSS/JS prettify 格式化代码

    1.首先安装插件 菜单的preference->packages control,然后输入install .. 回车,再输入HTML/CSS/JS prettify 再回车,重启后就可以了. 2 ...

  6. JAVA中单例模式的几种实现方式

    1 线程不安全的实现方法 首先介绍java中最基本的单例模式实现方式,我们可以在一些初级的java书中看到.这种实现方法不是线程安全的,所以在项目实践中如果涉及到线程安全就不会使用这种方式.但是如果不 ...

  7. 使用python来调试串口

    串口模块的波特率比较特别,找了几个串口工具都不支持...所以,干脆用python自己来写了,其实已经好奇好久了,别人的工具各种不顺手. 需要pyserial的支持,兼容各种平台,不需要新编译二进制文件 ...

  8. Ubuntu添加开机自动启动程序的方法

    文章出处:http://hi.baidu.com/gcc_gun/blog/item/fe9bbc4b84e911fa83025cb8.html 1. 开机启动时自动运行程序 Linux加载后, 它将 ...

  9. yield return 和 yield break

    //yield return 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 IEnumerator<T>. static I ...

  10. hdu 2044 一只小蜜蜂

    斐波那契数列变形,在本题中不是从1-N,而是从M-N 下标   1   2   3   4   5   6   7     8     9 值     1   1   2   3   5   8   ...