if __name__ == "__main__":
    usage = 'usage: websocket_chat -p pub address -s sub address port number'
    if len(sys.argv) != 6:
        print usage
        sys.exit(1)

    pub_addr = sys.argv[2]
    sub_addr = sys.argv[4]
    try:
        port = int(sys.argv[5])
    except ValueError:
        print "Error port supplied couldn't be converted to int\n", usage
        sys.exit(1)

    try:
        pub_socket = ctx.socket(zmq.PUB)
        pub_socket.connect(pub_addr)
        print "Publishing to %s" % pub_addr
        sub_socket = ctx.socket(zmq.SUB)
        sub_socket.connect(sub_addr)
        sub_socket.setsockopt(zmq.SUBSCRIBE, "")
        print "Subscribing to %s" % sub_addr
    except:
        print "Couldn't create sockets\n", usage
        sys.exit(1)

    spawn_n(subscribe_and_distribute, sub_socket)
    listener = evy.listen(('127.0.0.1', port))
    print "\nVisit http://localhost:%s/ in your websocket-capable browser.\n" % port
    wsgi.server(listener, dispatch)
示例#2
0
""" This is an incredibly simple port forwarder from port 7000 to 22 on 
localhost.  It calls a callback function when the socket is closed, to 
demonstrate one way that you could start to do interesting things by
starting from a simple framework like this.
"""

import evy


def closed_callback():
    print "called back"


def forward(source, dest, cb=lambda: None):
    """Forwards bytes unidirectionally from source to dest"""
    while True:
        d = source.recv(32384)
        if d == '':
            cb()
            break
        dest.sendall(d)


listener = evy.listen(('localhost', 7000))
while True:
    client, addr = listener.accept()
    server = evy.connect(('localhost', 22))
    # two unidirectional forwarders make a bidirectional one
    evy.spawn_n(forward, client, server, closed_callback)
    evy.spawn_n(forward, server, client)
示例#3
0
文件: forwarder.py 项目: inercia/evy
""" This is an incredibly simple port forwarder from port 7000 to 22 on 
localhost.  It calls a callback function when the socket is closed, to 
demonstrate one way that you could start to do interesting things by
starting from a simple framework like this.
"""

import evy

def closed_callback ():
    print "called back"


def forward (source, dest, cb = lambda: None):
    """Forwards bytes unidirectionally from source to dest"""
    while True:
        d = source.recv(32384)
        if d == '':
            cb()
            break
        dest.sendall(d)

listener = evy.listen(('localhost', 7000))
while True:
    client, addr = listener.accept()
    server = evy.connect(('localhost', 22))
    # two unidirectional forwarders make a bidirectional one
    evy.spawn_n(forward, client, server, closed_callback)
    evy.spawn_n(forward, server, client)
示例#4
0
    line = reader.readline()
    while line:
        print "Chat:", line.strip()
        for p in participants:
            try:
                if p is not writer: # Don't echo
                    p.write(line)
                    p.flush()
            except socket.error, e:
                # ignore broken pipes, they just mean the participant
                # closed its connection already
                if e[0] != 32:
                    raise
        line = reader.readline()
    participants.remove(writer)
    print "Participant left chat."

try:
    print "ChatServer starting up on port %s" % PORT
    server = evy.listen(('0.0.0.0', PORT))
    while True:
        new_connection, address = server.accept()
        print "Participant joined chat."
        new_writer = new_connection.makefile('w')
        participants.add(new_writer)
        evy.spawn_n(read_chat_forever,
                         new_writer,
                         new_connection.makefile('r'))
except (KeyboardInterrupt, SystemExit):
    print "ChatServer exiting."
示例#5
0
文件: zmq_chat.py 项目: inercia/evy
            who = line.split(':')[-1].strip()

        try:
            pub_socket.send_pyobj((who, line))
        except socket.error, e:
            # ignore broken pipes, they just mean the participant
            # closed its connection already
            if e[0] != 32:
                raise
        line = reader.readline()
    print "Participant left chat."

try:
    print "ChatServer starting up on port %s" % PORT
    server = evy.listen(('0.0.0.0', PORT))
    pub_socket = ctx.socket(zmq.PUB)
    pub_socket.bind(ADDR)
    evy.spawn_n(publish,
                     sys.stdout)
    while True:
        new_connection, address = server.accept()

        print "Participant joined chat."
        evy.spawn_n(publish,
                         new_connection.makefile('w'))
        evy.spawn_n(read_chat_forever,
                         new_connection.makefile('r'),
                         pub_socket)
except (KeyboardInterrupt, SystemExit):
    print "ChatServer exiting."
示例#6
0
if __name__ == "__main__":
    usage = 'usage: websocket_chat -p pub address -s sub address port number'
    if len(sys.argv) != 6:
        print usage
        sys.exit(1)

    pub_addr = sys.argv[2]
    sub_addr = sys.argv[4]
    try:
        port = int(sys.argv[5])
    except ValueError:
        print "Error port supplied couldn't be converted to int\n", usage
        sys.exit(1)

    try:
        pub_socket = ctx.socket(zmq.PUB)
        pub_socket.connect(pub_addr)
        print "Publishing to %s" % pub_addr
        sub_socket = ctx.socket(zmq.SUB)
        sub_socket.connect(sub_addr)
        sub_socket.setsockopt(zmq.SUBSCRIBE, "")
        print "Subscribing to %s" % sub_addr
    except:
        print "Couldn't create sockets\n", usage
        sys.exit(1)

    spawn_n(subscribe_and_distribute, sub_socket)
    listener = evy.listen(('127.0.0.1', port))
    print "\nVisit http://localhost:%s/ in your websocket-capable browser.\n" % port
    wsgi.server(listener, dispatch)
示例#7
0
文件: spawn_plot.py 项目: inercia/evy
def run_spawn_n_kw ():
    evy.spawn_n(dummy, i = 1)
示例#8
0
文件: spawn_plot.py 项目: inercia/evy
def run_spawn_n ():
    evy.spawn_n(dummy, 1)
示例#9
0
        print "Chat:", line.strip()
        if line.startswith('name:'):
            who = line.split(':')[-1].strip()

        try:
            pub_socket.send_pyobj((who, line))
        except socket.error, e:
            # ignore broken pipes, they just mean the participant
            # closed its connection already
            if e[0] != 32:
                raise
        line = reader.readline()
    print "Participant left chat."


try:
    print "ChatServer starting up on port %s" % PORT
    server = evy.listen(('0.0.0.0', PORT))
    pub_socket = ctx.socket(zmq.PUB)
    pub_socket.bind(ADDR)
    evy.spawn_n(publish, sys.stdout)
    while True:
        new_connection, address = server.accept()

        print "Participant joined chat."
        evy.spawn_n(publish, new_connection.makefile('w'))
        evy.spawn_n(read_chat_forever, new_connection.makefile('r'),
                    pub_socket)
except (KeyboardInterrupt, SystemExit):
    print "ChatServer exiting."
示例#10
0
    line = reader.readline()
    while line:
        print "Chat:", line.strip()
        for p in participants:
            try:
                if p is not writer:  # Don't echo
                    p.write(line)
                    p.flush()
            except socket.error, e:
                # ignore broken pipes, they just mean the participant
                # closed its connection already
                if e[0] != 32:
                    raise
        line = reader.readline()
    participants.remove(writer)
    print "Participant left chat."


try:
    print "ChatServer starting up on port %s" % PORT
    server = evy.listen(('0.0.0.0', PORT))
    while True:
        new_connection, address = server.accept()
        print "Participant joined chat."
        new_writer = new_connection.makefile('w')
        participants.add(new_writer)
        evy.spawn_n(read_chat_forever, new_writer,
                    new_connection.makefile('r'))
except (KeyboardInterrupt, SystemExit):
    print "ChatServer exiting."