python网络编程(基于twisted的客户端编程)
2016-01-12 19:43
671 查看
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
python的twisted比较有意思,既可以做server方面的编程,也可以做client方面的编程。关于这方面的编程,最简单的例子就是echo。
client 代码如下,
server 代码如下,
python的twisted比较有意思,既可以做server方面的编程,也可以做client方面的编程。关于这方面的编程,最简单的例子就是echo。
client 代码如下,
#!/usr/bin/python from twisted.internet.protocol import Protocol, ClientFactory from sys import stdout from twisted.internet import reactor class Echo(Protocol): def dataReceived(self, data): stdout.write(data) class EchoClientFactory(ClientFactory): def startedConnecting(self, connector): print 'Started to connect.' def buildProtocol(self, addr): print 'Connected.' return Echo() def clientConnectionLost(self, connector, reason): print 'Lost connection. Reason:', reason def clientConnectionFailed(self, connector, reason): print 'Connection failed. Reason:', reason if __name__ == '__main__': reactor.connectTCP('localhost', 1234, EchoClientFactory()) reactor.run()
server 代码如下,
#!/usr/bin/python from twisted.internet.protocol import Protocol, ClientFactory from sys import stdout from twisted.internet import reactor class Echo(Protocol): def dataReceived(self, data): stdout.write(data) class EchoClientFactory(ClientFactory): def startedConnecting(self, connector): print 'Started to connect.' def buildProtocol(self, addr): print 'Connected.' return Echo() def clientConnectionLost(self, connector, reason): print 'Lost connection. Reason:', reason def clientConnectionFailed(self, connector, reason): print 'Connection failed. Reason:', reason if __name__ == '__main__': reactor.connectTCP('localhost', 1234, EchoClientFactory()) reactor.run()
相关文章推荐
- C++基于TCP和UDP的socket通信
- OkHttpUtils的Eclipse版Lib
- HTTP 长连接和短连接
- iOS 网络请求
- http在request中放参数并在前台获取
- Android 网络开源库之-retrofit2.0
- jmeter 创建http请求
- 基于UDP的多线程网络数据包收发测试程序
- 基于UDP的单线程网络点对点数据包收发测试程序
- Android 主流网络图片加载缓存库框架Universal-ImageLoader的使用
- 安装nginx时安装http_stub_status_module
- http协议的实现 httpd的基础配置
- 人工智能之人工神经网络-纵览
- tcpkill清除异常tcp连接
- 利用dsniff的tcpkill杀TCP连接
- C#静态类 转载:(原文:http://www.cnblogs.com/chenlulouis/ )
- http://my.oschina.net/huangyong/blog/372491?fromerr=hobPLCmQ#OSC_h2_5
- Linux 系统应用编程——网络编程(socket编程)
- tcpdump捕包乱序的解决方案
- nginx 日志过滤网络爬虫