跳转到主要内容

PyF.Station是一个具有客户端和服务器实现的协议,用于在tcp网络上传输python生成器。

项目描述

简介
============

PyF.Station是一个具有客户端和服务器以在tcp网络上传输python生成器的协议。生成器中的项目必须是pyf.transport.Packet实例。

最佳实践是在第一个包中提供有关流的信息,该包被标识为头信息(例如包含认证数据、方法、目标等)。

错误在两端传递。

服务器
------

请注意,服务器需要tgscheduler(用于生成任务,传递生成器)和twisted。

示例:

from twisted.internet import reactor
from pyf.station import FlowServer

def sample_handler(flow, client=None)
header = flow.next()
print header
for i, item in enumerate(flow)
if not i%50
print i, item
print "end of flow..."
client.success("完成")

factory = FlowServer(sample_handler)
reactor.listenTCP(8000,factory)
reactor.run()

处理程序可以是如上例中的简单可调用对象,也可以是用户定义的类

from twisted.internet import reactor
from pyf.station import FlowServer

class SampleHandler(object)

def __init__(self, flow, client)
# __init__方法必须接受'flow'和'client'参数
self.flow = flow
self.client = client

def handle_data(self)
# 此方法将由FlowServer调用
header = self.flow.next()
print header
for i, item in enumerate(self.flow)
if not i%50
print i, item
print "end of flow..."
self.client.success("完成")

factory = FlowServer(SampleHandler)
reactor.listenTCP(8000,factory)
reactor.run()

另一个示例,如果您在一个已线程化的环境中(例如wsgi服务器):

from tgscheduler import scheduler
from twisted.internet import reactor
from pyf.station import FlowServer
from pyf.transport import Packet

def sample_handler(flow, client=None)
header = flow.next()
print header

for i, item in enumerate(flow)
# 每50个项目...
if not i%50
print i, item
# 我们向客户端发送一条消息
client.message(Packet({'type': 'info',
'message': 'hello ! (%s)' % i}))

print "end of flow..."
client.success("完成")

factory = FlowServer(sample_handler)
reactor.listenTCP(8000,factory)
scheduler.add_single_task(reactor.run,
kw=dict(installSignalHandlers=0),
initialdelay=0)

# 如果您是在wsgi应用程序中,可以注释掉这些行
scheduler.start_scheduler()
while True
pass

客户端
------

客户端示例:

from pyf.station import StationClient
from pyf.transport import Packet

client = StationClient('127.0.0.1', 8000, True)

def message_handler(message_packet)
# 服务器返回的消息的处理程序
print "消息处理程序被触发:%s" % message_packet

# 我们注册我们的回调
client.add_listener('message_received', message_handler)

# 我们生成示例数据包
flow = (Packet(dict(Field1=i+1,
Field2=('titi', 'tata')[i%2], num=i+1,
Field3=(i+1)*10))
for i in range(10000))

values = client.call(
flow,
header=dict(authtkt='my false auth token :)',
action='my_action'))
# 这里values要么是"True"(表示消息已成功传递),要么是从服务器返回的数据包。
for i, value in enumerate(values)
if not i % 5000
print i

if isinstance(value, Packet)
print value

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定要选择哪一个,请了解更多关于安装包的信息。

源代码分布

pyf.station-2.0.5.zip (12.8 kB 查看散列值)

上传时间 源代码

pyf.station-2.0.5.tar.gz (7.0 kB 查看散列值)

上传时间 源代码

构建分布

pyf.station-2.0.5-py2.7.egg (16.2 kB 查看散列值)

上传时间 源代码

pyf.station-2.0.5-py2-none-any.whl (10.6 kB 查看散列值)

上传时间 Python 2

由以下支持