- 浏览: 304112 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (69)
- python (31)
- android (1)
- 软件安装 (3)
- 运维监控 (5)
- pycurl (1)
- RFC (0)
- RFC,IRC,翻译 (1)
- ubuntu 打开 22端口 (1)
- redis (3)
- 缓存 (1)
- 多进程,共享数据,通信, (1)
- django (4)
- cookie (1)
- session (1)
- nodejs npm (1)
- mongodb (2)
- tornado (2)
- 异步 (1)
- seo (0)
- seo,错误,笔记 (1)
- sysv-rc-conf (1)
- ubuntu (2)
- 开机启动 (1)
- supervisor (1)
- 进程 (1)
- twitter tweepy (1)
- Bootstrap (1)
- Bootstrap-maxlength (1)
- 微信 (1)
- 公共号 (1)
- 框架 (1)
- mac,virtualbox,xp (1)
- beef (1)
- ruby1.8 (1)
- ruby1.9 (1)
- rvm (1)
- subline (1)
- egret (1)
- ssl (1)
最新评论
-
koreyoshi:
那要是周排名月排名呢
redis set轻松做排行榜应用 -
xuddk727:
仁兄能否将affinity_0.1.0发我一份?在我这该网站无 ...
python 绑定进程在某个cpu上执行-affinity -
mimicom:
wifi 调试 adb shell 的话 是 transpor ...
android adb shell 笔记 -
郭玉成:
一定要有SGMLParser.__init__(self) ...
用python解析html--SGMLParser -
jakejone:
支持一下,我刚开始学python,感觉不错,就是文档太少了。
python开发环境Eclipse+pydev字体和颜色设置方法(另附pydev安装方法)
#Email/gtalk:zealzpc@gmail.com
这是pyamf安装包里的例子,总的来说用pyamf来写server的各种例子还是很全的,包括django,gae,twisted,web2py等等,基本python常用的网络框架都有。现在很多网页游戏都是前端flash,那么和server端通信基本就用 amf了,如果server端的功能只是存储一些统计,或者对数据库的增删改查,那么一般的http短连接服务就能搞定了,以上提到的这些框架也都写起来比较简单,比如可以看django的例子http://www.pyamf.org/tutorials/gateways/django.html,寥寥数行就搞定了,python嘛。
但有时候需求不是这样的需要flash和server保持一个长连接来不断的进行通信改怎么搞呢?此时在pyamf的网站上看到了sockey这一段,正是我们想要的。
先看下server端的代码:server.py
# Copyright (c) The PyAMF Project. # See LICENSE.txt for details. """ Example socket server using Twisted. @see: U{Documentation for this example<http://pyamf.org/tutorials/actionscript/socket.html>} @since: 0.1 """ try: import twisted except ImportError: print "This examples requires the Twisted framework. Download it from http://twistedmatrix.com" raise SystemExit from twisted.internet.protocol import Protocol, Factory from twisted.internet import reactor from datetime import datetime import pyamf class TimerProtocol(Protocol): interval = 1.0 # 客户端链接到server后,server往客户端发送时间的间隔 encoding = pyamf.AMF3 timeout = 20 #客户端链接到server后多少时间不操作就断开链接的timeout def __init__(self): self.started = False #设置编码器 self.encoder = pyamf.get_encoder(self.encoding)、 #设置server端将数据编码成amf后存放的缓存地址 self.stream = self.encoder.stream def connectionLost(self, reason): Protocol.connectionLost(self, reason) print "locst connection:",reason #客户端没断开一个链接,总连接数-1 self.factory.number_of_connections -= 1 print "number_of_connections:",self.factory.number_of_connections def connectionMade(self): #如果服务器连接数超过最大连接数,拒绝新链接建立 if self.factory.number_of_connections >= self.factory.max_connections: self.transport.write('Too many connections, try again later') self.transport.loseConnection() return #总连接数+1 self.factory.number_of_connections += 1 self.timeout_deferred = reactor.callLater(TimerProtocol.timeout, self.transport.loseConnection) def dataReceived(self, data): #去除server收到client数据两端的空格 data = data.strip() #如果收到的是'start'命令 if data == 'start': # start sending a date object that contains the current time if not self.started: self.start() elif data == 'stop': self.stop() #每次执行完客户端请求后重置timeout,重新开始计算无操作时间。 if self.timeout_deferred: self.timeout_deferred.cancel() self.timeout_deferred = reactor.callLater(TimerProtocol.timeout, self.transport.loseConnection) def start(self): self.started = True self.sendTime() def stop(self): self.started = False def sendTime(self): if self.started: #往缓存流里写入信息,用编码器进行amf编码 self.encoder.writeElement(datetime.now()) #返回给客户端编码后的信息 self.transport.write(self.stream.getvalue()) #重置缓存流 self.stream.truncate() #每隔self.interval的时间再发送一次amf信息 reactor.callLater(self.interval, self.sendTime) class TimerFactory(Factory): protocol = TimerProtocol #最大链接数 max_connections = 1000 def __init__(self): self.number_of_connections = 0 class SocketPolicyProtocol(Protocol): """ Serves strict policy file for Flash Player >= 9,0,124. @see: U{http://adobe.com/go/strict_policy_files} """ def connectionMade(self): self.buffer = '' def dataReceived(self, data): self.buffer += data if self.buffer.startswith('<policy-file-request/>'): self.transport.write(self.factory.getPolicyFile(self)) self.transport.loseConnection() class SocketPolicyFactory(Factory): protocol = SocketPolicyProtocol def __init__(self, policy_file): """ @param policy_file: Path to the policy file definition """ self.policy_file = policy_file def getPolicyFile(self, protocol): return open(self.policy_file, 'rt').read() #设置域名,端口。 host = 'localhost' appPort = 8000 policyPort = 843 policyFile = 'socket-policy.xml' if __name__ == '__main__': from optparse import OptionParser #设置server启动选项 parser = OptionParser() parser.add_option("--host", default=host, dest="host", help="host address [default: %default]") parser.add_option("-a", "--app-port", default=appPort, dest="app_port", help="Application port number [default: %default]") parser.add_option("-p", "--policy-port", default=policyPort, dest="policy_port", help="Socket policy port number [default: %default]") parser.add_option("-f", "--policy-file", default=policyFile, dest="policy_file", help="Location of socket policy file [default: %default]") (opt, args) = parser.parse_args() print "Running Socket AMF gateway on %s:%s" % (opt.host, opt.app_port) print "Running Policy file server on %s:%s" % (opt.host, opt.policy_port) reactor.listenTCP(int(opt.app_port), TimerFactory(), interface=opt.host) reactor.listenTCP(int(opt.policy_port), SocketPolicyFactory(opt.policy_file), interface=opt.host) reactor.run()
里面最主要干活的就是TimerProtocol这个类。
在例子中还提供了另外一个文件:time.tac。懂twisted的人应该就知道了,这是一份twistd脚本,可以直接通过twisted自带的twistd来执行。里面import了 server.py中已经写好的TimerFactory,然后定义了一个application类,这样就可以通过twistd命令来执行了,twistd是什么可以参考官方文档http://twistedmatrix.com/documents/current/core/howto/application.html 本文就不细说了。
debug模式启动server:twistd -noy timer.tac
daemo模式启动server:twistd -oy timer.tac
你也可以直接启动server.py文件:python server.py、
区别就是第二种直接后台运行了在linux,你看不到报错和调试信息,当然windows上不行。
# Copyright (c) The PyAMF Project. # See LICENSE for details. import sys, os sys.path.append(os.getcwd()) from twisted.application import internet, service from server import TimerFactory, SocketPolicyFactory from server import appPort, policyPort timer = TimerFactory() policy = SocketPolicyFactory('socket-policy.xml') # this is the important bit application = service.Application('pyamf-socket-example') timerService = internet.TCPServer(appPort, timer) socketPolicyService = internet.TCPServer(policyPort, policy) timerService.setServiceParent(application) socketPolicyService.setServiceParent(application)
最后就是找个客户端来连接测测,例子里也提供了一个写好的flash,你打开通一个目录下的index.html就可以看看效果了。
例子里也给你准备了python的客户端。
# Copyright (c) The PyAMF Project. # See LICENSE for details. """ Python client for socket example. @since: 0.5 """ import socket import pyamf from server import appPort, host class AmfSocketClient(object): def __init__(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def connect(self, host, port): print "Connecting to socket server on %s:%d" % (host, port) try: self.sock.connect((host, port)) print "Connected to server.\n" except socket.error, e: raise Exception("Can't connect: %s" % e[1]) def start(self): msg = '' # tell server we started listening print "send request: start" try: self.sock.send('start') except socket.error, e: raise Exception("Can't connect: %s" % e[1]) while len(msg) < 1024: # read from server amf = self.sock.recv(1024) if amf == '': print "Connection closed." msg = msg + amf for obj in pyamf.decode(amf): print obj return msg def stop(self): print "send request: stop" self.sock.send('stop') if __name__ == '__main__': from optparse import OptionParser parser = OptionParser() parser.add_option("-p", "--port", default=appPort, dest="port", help="port number [default: %default]") parser.add_option("--host", default=host, dest="host", help="host address [default: %default]") (options, args) = parser.parse_args() host = options.host port = int(options.port) client = AmfSocketClient() client.connect(host, port) try: client.start() except KeyboardInterrupt: client.stop()
原文地址:http://www.pyamf.org/tutorials/actionscript/socket.html
代码就在pyamf安装包的pyamf / doc / tutorials / examples / actionscript / socket这个目录里。
欢迎讨论。
发表评论
-
一句话反射shell
2014-12-28 16:13 2860通过一句话反射一个shell 首先你在自己的及其上起 ... -
微信公共平台框架-python(支持多账号)
2014-04-15 15:39 4067email:zealzpc@gmail.com 因 ... -
进程管理工具 supervisor
2013-03-14 12:01 1651经常在服务器上会启动很多程序,然后重启一下又要一个个重新启 ... -
tornado 问题小计
2013-01-29 12:47 13581 tornaod在ubuntu 的安装目录 /usr/ ... -
django1.4 问题记录
2013-01-28 13:59 8671 ImportError: Settings canno ... -
聊聊 tornado 的异步回调
2013-01-17 10:47 13990异步回调程序的原理和写法我不就不介绍了,因为我主要是来吐槽下 ... -
小谈django 的 cookie和session
2012-11-12 00:03 9935本文针对django1.4 周末小闲在家研究了下dja ... -
小记 django 1.4的变化
2012-11-06 23:23 1550最近因为公司游戏功能逐渐到了收尾阶段也算空下来点了,就索性想尝 ... -
使用redisco轻松将python内置数据类型存入redis内
2012-07-26 23:12 9618我在之前的 <python使 ... -
python使用redis 神器 ---redisco(一)
2012-07-07 22:42 28597很久没认真推荐过东西了,实在是人懒也没以前那么有心思去研究 ... -
redis set轻松做排行榜应用
2012-05-31 20:23 15469Author:pako email:zealzpc@gmai ... -
永久修改python默认的字符编码为utf-8
2011-08-19 14:06 17669这个修改说来简单,其实不同的系统,修改起来还真不一样。下面来罗 ... -
ubuntu 上安装pycurl
2011-07-12 11:20 3094我 是 在 8。04上 安装的装了 easy_installl ... -
使用Twisted Application 框架
2011-06-20 15:12 6818翻译by:pako email/gtalk:zealzpc@ ... -
twisted:调用deferredlist多线程并发执行任务然后收集结果
2011-06-09 10:54 4477最近想通过twisted实现多线程并发去执行一件耗时的事,然后 ... -
对twisted 中deferred异步的理解
2011-03-29 15:38 4586最近小试了下twisted,还是发现用起来挺方便的,加之本身就 ... -
twisted简单实现多线程,轮询,后台daemon运行
2011-03-29 11:25 3231from twisted.application impo ... -
《django web开发指南》在v1.2.4版本上的出入
2011-01-25 00:44 1484<Django web 开发指南>第145页 第七 ... -
django.utils._os 中 safe_join 函数 windows下总报ValueError异常
2011-01-20 17:20 1768最近无聊在家看看django ... -
python 绑定进程在某个cpu上执行-affinity
2010-05-05 18:39 10517因为最近在看multiprocessing 所以想看看多进程在 ...
相关推荐
Twisted被设计为一个非常灵活的框架,可以使用它编写强大的服务器.这种灵活性的代价是当你编写服务器的时候需要分好几层. 本文档描述协议层,在这里定义协议的解析和处理.如果你正在编写一个应用的话这应该是你读的第...
主要介绍了python如何通过twisted搭建socket服务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
python twisted框架 服务器端 示例
这篇文章主要介绍了python如何通过twisted搭建socket服务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 服务端 # -*- coding: utf-8 -*- # @Time : 2018/9/19...
python twisted 简单服务器,服务器接收客户端发送的相应的信息,根据信息进行相应的返回数据
第一部分:Twisted理论基础 第二部分:异步编程初探与reactor模式 第三部分:初步认识Twisted 第四部分:由Twisted支持的诗歌客户端 第一个twisted支持的诗歌服务器 第一滴心血 第五部分:由Twisted支持的诗歌...
使用twisted开发的游戏服务器端,客户端为unity3d,请结合使用
Start by building basic TCP clients and servers, and then focus on deploying production-grade applications with the Twisted Application infrastructure. Along the way, you can play with and extend ...
Twisted comes with client and server implementations for all of its protocols, as well as utilities that make it easy to configure and deploy production-grade Twisted applications from the command ...
Start by building basic TCP clients and servers, and then focus on deploying production-grade applications with the Twisted Application infrastructure. Along the way, you can play with and extend ...
autobahn-python, 在 python 中为 Twisted 和 asyncio,web socket和, Autobahn|Pythonweb socket & WAMP用于 Twisted 和asyncio上的python 。 快速链接: 源代码 - 文档- web socket示例- web WebSocke
Twisted是用于Internet应用程序的基于事件的框架,支持Python 2.7和Python 3.5+...twisted.names:DNS客户端和用于创建自己的DNS服务器的工具 twisted.trial:一种单元测试框架,可以与基于Twisted的代码很好地集成。
我学习和使用twisted已经好几年了,通过这几年的学习和工作我得出的结论就是:学习twisted困难的地方就是对异步编程的理解而不是怎样去用twisted 的函数去写代码. twisted 的代码写的都很简洁和清晰,而且有很好的注释...
Twisted是用Python实现的基于事件驱动的网络引擎框架。Twisted诞生于2000年初,在当时的网络游戏开发者看来,无论他们使用哪种语言,手中都鲜有可兼顾扩展性及跨平台的网络库。Twisted的作者试图在当时现有的环境下...
Twisted是用Python实现的基于事件驱动的网络引擎框架。Twisted诞生于2000年初,在当时的网络游戏开发者看来,无论他们使用哪种语言,手中都鲜有可兼顾扩展性及跨平台的网络库。Twisted的作者试图在当时现有的环境下...
Twisted Info Twisted入门教程源码
twisted资料twisted资料twisted资料
Twisted与异步编程入门,最好的入门资源。