当前位置: 首页>>代码示例>>Python>>正文


Python Application.add_service方法代码示例

本文整理汇总了Python中diesel.Application.add_service方法的典型用法代码示例。如果您正苦于以下问题:Python Application.add_service方法的具体用法?Python Application.add_service怎么用?Python Application.add_service使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在diesel.Application的用法示例。


在下文中一共展示了Application.add_service方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: len

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
# vim:ts=4:sw=4:expandtab
'''The oh-so-canonical "Hello, World!" http server.
'''
from diesel import Application, Service
from diesel.protocols import http

# Pre-gen, since it's static.. 
content = "Hello, World!"
headers = http.HttpHeaders()
headers.add('Content-Length', len(content))
headers.add('Content-Type', 'text/plain')

def hello_http(req):
    return http.http_response(req, 200, headers, content)

app = Application()
app.add_service(Service(http.HttpServer(hello_http), 8088))
app.run()
开发者ID:dowski,项目名称:aspen,代码行数:20,代码来源:http.py

示例2: web_handler

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
<input type="text" size="40" id="the-i" /> <input type="button" value="Update Message" onclick="push(); return false" />

</body>
</html>
''' % LOCATION

def web_handler(req):
    heads = HttpHeaders()
    heads.add('Content-Length', len(content))
    heads.add('Content-Type', 'text/html')

    return http_response(req, 200, heads, content)

import time

def socket_handler(inq, outq):
    message = "hello, there!"
    while True:
        try:
            v = inq.get(timeout=0.5)
        except QueueTimeout:
            pass
        else:
            message = v['message']
                        
        outq.put(WSD(message=message, time=time.time()))

a = Application()
a.add_service(Service(WebSocketServer(web_handler, socket_handler, LOCATION), 8091))
a.run()
开发者ID:dowski,项目名称:aspen,代码行数:32,代码来源:ws.py

示例3: hi_server

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
# vim:ts=4:sw=4:expandtab
'''Simple echo server.
'''
from diesel import Application, Service, until_eol, send

def hi_server(addr):
    while 1:
        inp = until_eol()
        if inp.strip() == "quit":
            break
        send("you said %s" % inp)

app = Application()
app.add_service(Service(hi_server, 8013))
app.run()
开发者ID:FashtimeDotCom,项目名称:diesel,代码行数:17,代码来源:echo.py

示例4: delay_echo_server

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
# vim:ts=4:sw=4:expandtab
'''Demonstrate sleep-type behavior server-side.
'''
from diesel import Application, Service, until_eol, sleep, send

def delay_echo_server(addr):
    inp = until_eol()

    for x in xrange(4):
        sleep(2)
        send(str(x) + '\r\n')
    send("you said %s" % inp)

app = Application()
app.add_service(Service(delay_echo_server, 8013))
app.run()
开发者ID:FashtimeDotCom,项目名称:diesel,代码行数:18,代码来源:sleep_server.py

示例5: thread

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
            msg = thread(self.read_chat_message, "").strip()
            self.input.put(msg)

    @call
    def chat(self):
        fork(self.input_handler)
        nick = self.input.get()
        send("%s\r\n" % nick)
        while True:
            evt, data = first(until_eol=True, waits=[self.input])
            if evt == "until_eol":
                print data.strip()
            else:
                send("%s\r\n" % data)


def chat_client():
    with ChatClient("localhost", 8000) as c:
        c.chat()


app = Application()
if sys.argv[1] == "server":
    app.add_service(Service(chat_server, 8000))
elif sys.argv[1] == "client":
    app.add_loop(Loop(chat_client))
else:
    print "USAGE: python %s [server|client]" % sys.argv[0]
    raise SystemExit(1)
app.run()
开发者ID:1angxi,项目名称:diesel,代码行数:32,代码来源:consolechat.py

示例6: until

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
        message = yield until("\r\n")
        yield "you said: %s" % message


class EchoClient(Client):
    @call
    def echo(self, message):
        yield message + "\r\n"
        back = yield until("\r\n")
        yield response(back)


app = Application()


def do_echos():
    client = EchoClient()
    yield client.connect("localhost", 8000)
    t = time.time()
    for x in xrange(5000):
        msg = "hello, world #%s!" % x
        echo_result = yield client.echo(msg)
        assert echo_result.strip() == "you said: %s" % msg
    print "5000 loops in %.2fs" % (time.time() - t)
    app.halt()


app.add_service(Service(handle_echo, port=8000))
app.add_loop(Loop(do_echos))
app.run()
开发者ID:Bobberino,项目名称:musings,代码行数:32,代码来源:diesel_test.py

示例7: delay_echo_server

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
# vim:ts=4:sw=4:expandtab
'''Non blocking memcache get/set.
'''
from diesel import Application, Service
from diesel.protocols.memcache import MemCacheClient
from diesel.protocols import http
from diesel import log

def delay_echo_server(addr):
    # default to 127.0.0.1:11211
    m = MemCacheClient('localhost')
    value = m.get('mykey')
    values = m.get_multi(['mykey', 'mykey1', 'mykey2'])
    
    return http.Response("value from multi_get : %s and get : %s"%(str(values), str(value)))
    #return http.Response("value from memcache key : %s key1 : %s and key 2 : %s"%(str(value), str(value1), str(value2)))

app = Application()
app.add_service(Service(http.HttpServer(delay_echo_server), 8000))
app.run()
开发者ID:pranjal5215,项目名称:diesel,代码行数:22,代码来源:memcache_get.py

示例8: sleep

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
        yield sleep(8)
        yield c.sub.test.update({'name':'allrooms'}, {'name':'allrooms', 'value':['foo', 'bar', 'baz']}, upsert=1)

    def wait_for_doc_update(req):
        c = SubscribingClient(id='foo-sub')
        yield c.connect(BACKEND_HOST, FRONTEND_PORT)
        yield c.bub.foo.subscribe({'junk':'no'})
        val = str((yield c.bub.foo.wait()))
        headers = http.HttpHeaders()
        headers.add('Content-Length', len(val))
        headers.add('Content-Type', 'text/plain')
        yield http.http_response(req, 200, headers, val)

    def main():
        c = MongoClient()
        yield c.connect(BACKEND_HOST, FRONTEND_PORT)
        yield c.drop_database('sub')
        yield c.drop_database('bub')
        print "main: dropped the db"
        a.add_loop(Loop(subscriber))
        a.add_loop(Loop(publisher))
        print "main: loops started"
        c.close()

    a = Application()
    a.add_service(Service(SubscriptionProxy(BACKEND_HOST, BACKEND_PORT), FRONTEND_PORT))
    a.add_service(Service(http.HttpServer(wait_for_doc_update), 8088))
    a.add_loop(Loop(main))
    a.run()

开发者ID:viraptor,项目名称:diesel,代码行数:31,代码来源:mongodbproxy.py

示例9: until

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
        message = until('\r\n')
        send("you said: %s" % message)

class EchoClient(Client):
    @call
    def echo(self, message):
        send(message + '\r\n')
        back = until("\r\n")
        return back

app = Application()
log = log.sublog('echo-system', log.info)

def do_echos():
    client = EchoClient('localhost', 8000, ssl_ctx=SSL.Context(SSL.TLSv1_METHOD))
    t = time.time()
    for x in xrange(5000):
        msg = "hello, world #%s!" % x
        echo_result = client.echo(msg)
        assert echo_result.strip() == "you said: %s" % msg
    log.info('5000 loops in %.2fs' % (time.time() - t))
    app.halt()

server_ctx = SSL.Context(SSL.TLSv1_METHOD)
server_ctx.use_privatekey_file('snakeoil-key.pem')
server_ctx.use_certificate_file('snakeoil-cert.pem')
app.add_service(Service(handle_echo, port=8000, ssl_ctx=server_ctx))

app.add_loop(Loop(do_echos))
app.run()
开发者ID:dowski,项目名称:aspen,代码行数:32,代码来源:combined_tls.py

示例10: hi_loop

# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_service [as 别名]
# vim:ts=4:sw=4:expandtab
'''Simple udp echo client.
'''
import time
from diesel import Application, UDPService, UDPLoop, send, sleep

def hi_loop():
    while 1:
        send("whatup?", addr='localhost', port=8013)
        print time.ctime(), "sent message to server"
        sleep(3)

def hi_client(data, addr):
    print time.ctime(), "remote service said '%s'" % data

app = Application()
app.add_service(UDPService(hi_client, 8014))
app.add_loop(UDPLoop(hi_loop))
app.run()
开发者ID:timdoug,项目名称:diesel,代码行数:21,代码来源:udp_echo_client.py


注:本文中的diesel.Application.add_service方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。