Exemplo n.º 1
0
    def __init__(self, server_port, backlog, irc_address, irc_port):
        self.irc_address = irc_address
        self.irc_port    = irc_port

        server = Spin()
        server.bind(('', server_port))
        server.listen(int(backlog))
    
        Server(server)
    
        xmap(server, ACCEPT, self.handle_accept)
Exemplo n.º 2
0
    def __init__(self, server_port, backlog, irc_address, irc_port):
        self.irc_address = irc_address
        self.irc_port = irc_port

        server = Spin()
        server.bind(('', server_port))
        server.listen(int(backlog))

        Server(server)

        xmap(server, ACCEPT, self.handle_accept)
Exemplo n.º 3
0
def create_server(addr, port, backlog):
    """
    Set up a TCP server and installs the basic handles Stdin, Stdout in the
    clients.

    Example:    

    def send_data(server, client):
        # No need to install Stdin or Stdout.
        client.dump('foo bar!')

    server = create_server('0.0.0.0', 1024, 50)
    xmap(server, on_accept, send_data)
    """

    server = Spin()
    server.bind((addr, port))
    server.listen(backlog)
    Server(server)
    server.add_map(ACCEPT, lambda server, spin: install_basic_handles(spin))
    return server
Exemplo n.º 4
0
    def on_div(self, client, args):
        self.send_msg(client, reduce(operator.truediv, args, args.pop(0)))

    def on_mul(self, client, args):
        self.send_msg(client, reduce(operator.mul, args, args.pop(0)))

    def on_error(self, client, excpt):
        self.send_msg(client, excpt)

    def handle_close(self, client, err):
        client.destroy()
        client.close()

    def send_msg(self, client, msg):
        client.dump(('%s\r\n' % msg).encode('utf8'))

if __name__ == '__main__':
    server = Spin()
    server.bind(('', 1234))
    server.listen(5)

    Server(server)
    CalcServer(server)
    core.gear.mainloop()






Exemplo n.º 5
0
    Stdin(con)
    Stdout(con)
    xmap(con, CLOSE, lambda con, err: lose(con))
    xmap(con, LOAD, lambda con, data: sys.stdout.write('%s\r\n' % data))


if __name__ == '__main__':
    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option("-a",
                      "--addr",
                      dest="addr",
                      metavar="string",
                      default='0.0.0.0')

    parser.add_option("-p", "--port", dest="port", type="int", default=1234)

    parser.add_option("-b", "--backlog", dest="backlog", type="int", default=5)

    (opt, args) = parser.parse_args()
    sock = socket(AF_INET, SOCK_STREAM)
    server = Spin(sock)
    server.bind((opt.addr, opt.port))
    server.listen(opt.backlog)
    Server(server)

    xmap(server, ACCEPT, setup)

    core.gear.mainloop()
Exemplo n.º 6
0
    from optparse import OptionParser

    parser   = OptionParser()
    parser.add_option("-a", "--addr", dest="addr",
                      metavar="string", default='0.0.0.0')
                  
    parser.add_option("-p", "--port", dest="port",
                      type="int", default=1234)

    parser.add_option("-b", "--backlog", dest="backlog",
                      type="int", default=5)

    (opt, args) = parser.parse_args()
    sock   = socket(AF_INET, SOCK_STREAM)
    server = Spin(sock) 
    server.bind((opt.addr, opt.port))
    server.listen(opt.backlog)
    Server(server)
    
    xmap(server, ACCEPT, setup)
    
    core.gear.mainloop()








Exemplo n.º 7
0
# Name: bar.py
# Description: It sets a server socket and prints everything
# the clients send to the server.

from untwisted.network import xmap, Spin, core
from untwisted.utils.stdio import Server, Stdout, lose, ACCEPT, LOAD, CLOSE
from socket import *
import sys


def set_up_con(server, con):
    Stdout(con)
    xmap(con, CLOSE, lambda con, err: lose(con))
    xmap(con, LOAD, lambda con, data: sys.stdout.write('%s\n' % data))


sock = socket(AF_INET, SOCK_STREAM)
server = Spin(sock)
server.bind(('', 1234))
server.listen(2)
Server(server)

xmap(server, ACCEPT, set_up_con)

core.gear.mainloop()