Ejemplo n.º 1
0
def create_connection(addr, port, msg):
    sock = socket(AF_INET, SOCK_STREAM)
    con = Spin(sock)

    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, setup, msg)
Ejemplo n.º 2
0
def create_connection(addr, port):
    sock = socket(AF_INET, SOCK_STREAM)
    spin = Spin(sock)
    Client(spin)
    xmap(spin, CONNECT, is_open, port)
    spin.connect_ex((addr, port))
    return spin
Ejemplo n.º 3
0
def create_connection(addr, port, msg):
    sock = socket(AF_INET, SOCK_STREAM)
    con  = Spin(sock)

    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, setup, msg)
Ejemplo n.º 4
0
    def __init__(self, area, addr, port):
        con = Spin()
        con.connect_ex((addr, int(port)))
        Client(con)

        xmap(con, CONNECT, lambda con: self.set_up_con(con, area))
        xmap(con, CONNECT_ERR, self.on_connect_err)
Ejemplo n.º 5
0
def create_connection(addr, port):
    sock = socket(AF_INET, SOCK_STREAM)
    spin = Spin(sock)
    Client(spin)
    xmap(spin, CONNECT, is_open, port)
    spin.connect_ex((addr, port))
    return spin
Ejemplo n.º 6
0
    def handle_accept(self, server, client):
        irc = Spin()
        Client(irc)
        xmap(irc, CONNECT, self.handle_connect, client)
        xmap(irc, CONNECT_ERR, self.down_connection)

        irc.connect_ex((self.irc_address, self.irc_port))
Ejemplo n.º 7
0
def create_connection(addr, port):
    con = Spin()
    Client(con)
    xmap(con, CONNECT, on_connect, addr, port)
    xmap(con, CONNECT_ERR, on_connect_err, addr, port)
    xmap(con, CONNECT, lambda con: die())
    xmap(con, CONNECT_ERR, lambda con, err: die())
    
    con.connect_ex((addr, port))
Ejemplo n.º 8
0
def create_connection(addr, port):
    con = Spin()
    Client(con)
    xmap(con, CONNECT, on_connect, addr, port)
    xmap(con, CONNECT_ERR, on_connect_err, addr, port)
    xmap(con, CONNECT, lambda con: die())
    xmap(con, CONNECT_ERR, lambda con, err: die())
    
    con.connect_ex((addr, port))
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
def kpost(rsc, username, password, payload):
    ADDR = 'baas.kinvey.com'
    PORT = 80
    HEADER = {'Host': '%s' % ADDR, 'Authorization': auth(username, password)}

    con = Spin()
    data = post_data(rsc, data=json.loads(payload), header=HEADER)

    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    return con
Ejemplo n.º 11
0
def connect(rsc, username, password):
    ADDR = 'baas.kinvey.com'
    PORT = 80
    HEADER = {'Host': '%s' % ADDR, 'Authorization': auth(username, password)}

    con = Spin()
    data = get(rsc, header=HEADER)

    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
Ejemplo n.º 12
0
    def __init__(self):
        server = socket(AF_INET, SOCK_STREAM)
        server.bind(('127.0.0.1', 0))
        server.listen(1)

        client = Spin()
        client.connect_ex(server.getsockname())

        def consume(spin):
            spin.recv(self.MAX_SIZE)

        client.add_map(READ, consume)

        self.con, addr  = server.accept()
        self.lock       = Lock()
Ejemplo n.º 13
0
    def __init__(self, addr, port, user, nick, irccmd, channels=[]):
        con = Spin()
        self.con = con
        con.connect_ex((addr, int(port)))
        Client(con)

        xmap(con, CONNECT, self.on_connect)
        xmap(con, CONNECT_ERR, self.e_connect_err)
        self.misc = None
        self.addr = addr
        self.port = port
        self.user = user
        self.nick = nick
        self.irccmd = irccmd
        self.channels = channels
Ejemplo n.º 14
0
def kpost(rsc, username, password, payload):
    ADDR   = 'baas.kinvey.com'
    PORT   = 80
    HEADER = {
    'Host':'%s' % ADDR,
    'Authorization': auth(username, password)
    }
        
    con  = Spin()
    data = post_data(rsc, data=json.loads(payload), header=HEADER)
    
    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    return con
Ejemplo n.º 15
0
def connect(addr, port, rsc):
    HEADER = {
        'Host': '%s' % addr,
        'User-Agent': "uxreq/1.0",
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
        'Connection': 'close',
    }

    con = Spin()
    data = get(rsc, header=HEADER)

    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
Ejemplo n.º 16
0
def connect(rsc, username, password):
    ADDR   = 'baas.kinvey.com'
    PORT   = 80
    HEADER = {
    'Host':'%s' % ADDR,
    'Authorization': auth(username, password)
    }
        
    con  = Spin()
    data = get(rsc, header=HEADER)
    
    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
Ejemplo n.º 17
0
def connect(addr, port, rsc):
    HEADER = {
    'Host':'%s' % addr,
    'User-Agent':"uxreq/1.0", 
    'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
    'Connection':'close',
    }
        
    
    con  = Spin()
    data = get(rsc, header=HEADER)
    
    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
Ejemplo n.º 18
0
    def __init__(self):
        # I have to improve this code.
        # i have to handle the exception
        # if the port is in use etc. 
     
        Spin.__init__(self)
        server = socket(AF_INET, SOCK_STREAM)
        server.bind(('127.0.0.1', 0))
        server.listen(1)
        self.connect_ex(server.getsockname())

        sock, addr = server.accept()
        self.sock  = sock
        server.close()

        xmap(self, READ, self.dispatch)
        
        self.lock  = Lock()
        self.ident = None
        self.data  = None
Ejemplo n.º 19
0
    def __init__(self):
        # I have to improve this code.
        # i have to handle the exception
        # if the port is in use etc.

        Spin.__init__(self)
        server = socket(AF_INET, SOCK_STREAM)
        server.bind(("127.0.0.1", 0))
        server.listen(1)
        self.connect_ex(server.getsockname())

        sock, addr = server.accept()
        self.sock = sock
        server.close()

        xmap(self, READ, self.dispatch)

        self.lock = Lock()
        self.ident = None
        self.data = None
Ejemplo n.º 20
0
    def handle_accept(self, server, client):
        Stdin(client)
        Stdout(client)
        Terminator(client, delim='\r\n')

        xmap(client, Terminator.FOUND, self.handle_found)
        xmap(client, CLOSE, self.down_connection)

        irc = Spin()
        Client(irc)
        Stdin(irc)
        Stdout(irc)
        Shrug(irc, delim='\r\n')
        xmap(irc, Terminator.FOUND, self.handle_found)
        xmap(irc, CONNECT, self.handle_connect)
        xmap(irc, CONNECT_ERR, self.down_connection)
        xmap(irc, CLOSE, self.down_connection)

        irc.arrow = client
        client.arrow = irc

        irc.connect_ex((self.irc_address, self.irc_port))
Ejemplo n.º 21
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)
Ejemplo n.º 22
0
Archivo: app.py Proyecto: iogf/steinitz
    def plug(self):
        # If it is connected then we have to unplug.
        if self.con: self.unplug()
        host, port = self.ask_address()         

        # We create our connection socket.
        sock = socket(AF_INET, SOCK_STREAM)

        # It wraps the socket so we can install protocols into it.
        self.con = Spin(sock)
        
        Client(self.con)
       
        # It maps CONNECT to send_ident.
        self.con.add_map(CONNECT, self.send_ident) 
        self.con.add_map(CONNECT_ERR, lambda con, err: self.update_text(con, 'Connection failed.'))
        self.con.add_map(CONNECT_ERR, lambda con, err: lose(con))

        self.con.connect_ex((host, port))
Ejemplo n.º 23
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
Ejemplo n.º 24
0
    def update(self, spin):
        # Calls repeatedly accept to improve efficience.
        while True:
            try:
                sock, addr = spin.accept()

                # Instantiate a new Spin and spreads it.
                # Since it is connected is_on=True
                new = Spin(sock)
                spawn(spin, ACCEPT, new)

            except socket.error as excpt:
                # If there is no client accept throws
                # an exception that is spreaded too.
                err = excpt.args[0]
                if not err in ACCEPT_ERR_CODE:
                    # It spawns ACCEPT_ERR just if the socket
                    # it isn't a peculiar set of conditions.
                    # As we are calling accpet inside a while
                    # we would have continously EAGAIN, EWOULDBLOCK etc.
                    spawn(spin, ACCEPT_ERR, err)
                    debug()
                else:
                    break
Ejemplo n.º 25
0
def create_client(addr, port):
    """
    Set up a TCP client and installs the basic handles Stdin, Stdout.

    def send_data(client):
        client.dump('GET / HTTP/1.1\r\n')
        xmap(client, LOAD, iostd.put)

    client = create_client('www.google.com.br', 80)
    xmap(client, CONNECT, send_data)
    """
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # First attempt to connect otherwise it leaves
    # an unconnected spin instance in the reactor.
    sock.connect_ex((addr, port))

    spin = Spin(sock)
    Client(spin)
    spin.add_map(CONNECT, install_basic_handles)
    spin.add_map(CONNECT_ERR, lambda con, err: lose(con))
    return spin
Ejemplo n.º 26
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()






Ejemplo n.º 27
0
def connect(servaddr, port, nick, user, nick_passwd, adm_passwd, chan_list, plugmap):
    sock   = socket(AF_INET, SOCK_STREAM)
    ip     = gethostbyname(servaddr)
    server = Spin(sock)
    Client(server)

    def auto_join(server, *args):
        send_cmd(server, nick_passwd)
        for ind in chan_list:
            send_cmd(server, 'JOIN %s' % ind)

    def get_myaddr(server, servaddr, nick, msg):
        server.nick = nick
        send_cmd(server, 'USERHOST %s' % nick)
        _, args        = yield hold(server, '302')
        _, _, _, ident = args
        user, myaddr   = ident.split('@')
        server.myaddr  = myaddr

    def update_nick(server, nick_x, user, host, nick_y):
        if server.nick == nick_x:
            server.nick = nick_y
    
    def handle_connect(server):
        Stdin(server)
        Stdout(server)
        Shrug(server)

        Irc(server)
        CTCP(server)

        Misc(server)
        adm.install(server)

        xmap(server, 'PING', lambda server, prefix, servaddr: 
                send_cmd(server, 'PONG :%s' % servaddr))
        
        xmap(server, CLOSE, lambda server, err: lose(server))
        logcon(server)

        xmap(server, '376', auto_join)
        xmap(server, '376', get_myaddr)
        xmap(server, 'NICK', update_nick)
        plugmap(server)

        server.servaddr    = servaddr
        server.port        = port
        server.nick        = nick
        server.user        = user
        server.chan_list   = chan_list
        server.nick_passwd = nick_passwd
        server.adm_passwd  = adm_passwd
        server.plugmap     = plugmap

        send_cmd(server, 'NICK %s' % nick)
        send_cmd(server, 'USER %s' % user) 
    
    xmap(server, CONNECT, handle_connect)
    xmap(server, CONNECT_ERR, lambda server, err: lose(server))

    server.connect_ex((ip, port))
    return server
Ejemplo n.º 28
0
# Name: foo.py
# Description: This example connects to a local socket it sends
# a chunk of text and disconnects.

from untwisted.network import xmap, Spin, core
from untwisted.utils.stdio import Client, Stdin, CONNECT, DUMPED, lose
from socket import *

def set_up_con(con):
# We install Stdin to be able of sending data.
    Stdin(con)
# When everything is sent it just disconnects.
    xmap(con, DUMPED, lose)

# Stdin installs dump method in con. 
# So, we can use it to send data.
    con.dump('Hello world\r\n')

sock = socket(AF_INET, SOCK_STREAM)
con = Spin(sock)
Client(con)
con.connect_ex(('localhost', 1234))
xmap(con, CONNECT, set_up_con)

core.gear.mainloop()
Ejemplo n.º 29
0
# Name: foo.py
# Description: This example connects to a local socket it sends
# a chunk of text and disconnects.

from untwisted.network import xmap, Spin, core
from untwisted.utils.stdio import Client, Stdin, CONNECT, DUMPED, lose
from socket import *


def set_up_con(con):
    # We install Stdin to be able of sending data.
    Stdin(con)
    # When everything is sent it just disconnects.
    xmap(con, DUMPED, lose)

    # Stdin installs dump method in con.
    # So, we can use it to send data.
    con.dump('Hello world\r\n')


sock = socket(AF_INET, SOCK_STREAM)
con = Spin(sock)
Client(con)
con.connect_ex(('localhost', 1234))
xmap(con, CONNECT, set_up_con)

core.gear.mainloop()
Ejemplo n.º 30
0
        self.send_msg(client, reduce(operator.add, args, 0))

    def on_sub(self, client, args):
        self.send_msg(client, reduce(operator.sub, args, 0))

    def on_div(self, client, args):
        self.send_msg(client, reduce(operator.div, 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)


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

    Server(server)
    CalcServer(server)
    core.gear.mainloop()
Ejemplo n.º 31
0
from builtins import object
from untwisted.network import core, Spin, xmap
from untwisted.iostd import Server, Stdin, ACCEPT, CLOSE
from time import asctime

class TimeServ(object):
    def __init__(self, server):
        xmap(server, ACCEPT, self.handle_accept)
       

    def handle_accept(self, server, con):
        Stdin(con)

        con.dump('%s\r\n' % asctime())
        xmap(con, CLOSE, lambda con, err: lose(con))


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

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





Ejemplo n.º 32
0
Archivo: app.py Proyecto: iogf/steinitz
class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        # It is interesting to hold the root instance
        # though most of times you will not need it.

        # At this point we have no connection initialized
        # we just assign None to self.con to map this state.
        self.con = None

        setting = shelve.open(os.path.join(os.path.expanduser('~'), '.snz'))
        self.stockfish_depth = setting.get('depth', 10)
        self.stockfish_path = setting.get('path', 'stockfish')
        setting.close()
    
        self.stockfish = Expect('stockfish')
        Terminator(self.stockfish, delim='\n')
        stockfish.install(self.stockfish)

        def quit():
            self.stockfish.terminate()
            self.destroy()

        self.protocol('WM_DELETE_WINDOW', quit)

        self.title('Steinitz')
        self.menubar = Menu(master=self)
        self.config(menu=self.menubar)

        self.menu1 = Menu(self.menubar, tearoff = 0)

        self.submenu1 = Menu(self.menubar, tearoff = 0)
        self.menu1.add_cascade(label='Rated White', menu=self.submenu1)

        self.submenu1.add_command(label='Blitz 3 0', 
                                  command=lambda: self.con.dump('seek 3 0 rated white formula\r\n'))
        self.submenu1.add_command(label='Blitz 5 0', 
                                  command=lambda: self.con.dump('seek 5 0 rated white formula\r\n'))
        self.submenu1.add_separator()
        self.submenu1.add_command(label='Standard 10 0', 
                                  command=lambda: self.con.dump('seek 10 0 rated white formula\r\n'))
        self.submenu1.add_command(label='Standard 15 0', 
                                  command=lambda: self.con.dump('seek 15 0 rated white formula\r\n'))
        self.submenu1.add_command(label='Standard 20 0',
                                  command=lambda: self.con.dump('seek 20 0 rated white formula\r\n'))
        self.submenu1.add_command(label='Standard 30 0',
                                  command=lambda: self.con.dump('seek 30 0 rated white formula\r\n'))
        self.submenu1.add_command(label='Standard 60 0', 
                                  command=lambda: self.con.dump('seek 60 0 rated white formula\r\n'))
        self.submenu2 = Menu(self.menubar, tearoff = 0)
        self.menu1.add_cascade(label='Unrated White', menu=self.submenu2)
        self.submenu2.add_command(label='Blitz 3 0', 
                                  command=lambda: self.con.dump('seek 3 0 unrated white formula\r\n'))
        self.submenu2.add_command(label='Blitz 5 0', 
                                  command=lambda: self.con.dump('seek 5 0 unrated white formula\r\n'))
        self.submenu2.add_separator()
        self.submenu2.add_command(label='Standard 10 0', 
                                  command=lambda: self.con.dump('seek 10 0 unrated white formula\r\n'))
        self.submenu2.add_command(label='Standard 15 0',
                                  command=lambda: self.con.dump('seek 15 0 unrated white formula\r\n'))
        self.submenu2.add_command(label='Standard 20 0',
                                  command=lambda: self.con.dump('seek 20 0 unrated white formula\r\n'))
        self.submenu2.add_command(label='Standard 30 0', 
                                  command=lambda: self.con.dump('seek 30 0 unrated white formula\r\n'))
        self.submenu2.add_command(label='Standard 60 0',
                                  command=lambda: self.con.dump('seek 60 0 unrated white formula\r\n'))
        self.menu1.add_separator()
        self.submenu3 = Menu(self.menubar, tearoff = 0)
        self.menu1.add_cascade(label='Rated Black', menu=self.submenu3)
        self.submenu3.add_command(label='Blitz 3 0', 
                                  command=lambda: self.con.dump('seek 3 0 rated black formula\r\n'))
        self.submenu3.add_command(label='Blitz 5 0', 
                                  command=lambda: self.con.dump('seek 5 0 rated black formula\r\n'))
        self.submenu3.add_separator()
        self.submenu3.add_command(label='Standard 10 0', 
                                  command=lambda: self.con.dump('seek 10 0 rated black formula\r\n'))
        self.submenu3.add_command(label='Standard 15 0',
                                  command=lambda: self.con.dump('seek 15 0 rated black formula\r\n'))
        self.submenu3.add_command(label='Standard 20 0', 
                                  command=lambda: self.con.dump('seek 20 0 rated black formula\r\n'))
        self.submenu3.add_command(label='Standard 30 0', 
                                  command=lambda: self.con.dump('seek 30 0 rated black formula\r\n'))
        self.submenu3.add_command(label='Standard 60 0', 
                                  command=lambda: self.con.dump('seek 60 0 rated black formula\r\n'))


        self.submenu4 = Menu(self.menubar, tearoff = 0)
        self.menu1.add_cascade(label='Unrated Black', menu=self.submenu4)

        self.submenu4.add_command(label='Blitz 3 0', 
                                  command=lambda: self.con.dump('seek 3 0 unrated black formula\r\n'))
        self.submenu4.add_command(label='Blitz 5 0', 
                                  command=lambda: self.con.dump('seek 5 0 unrated black formula\r\n'))
        self.submenu4.add_separator()
        self.submenu4.add_command(label='Standard 10 0', 
                                  command=lambda: self.con.dump('seek 10 0 unrated black formula\r\n'))
        self.submenu4.add_command(label='Standard 15 0',
                                  command=lambda: self.con.dump('seek 15 0 unrated black formula\r\n'))
        self.submenu4.add_command(label='Standard 20 0', 
                                  command=lambda: self.con.dump('seek 20 0 unrated black formula\r\n'))
        self.submenu4.add_command(label='Standard 30 0', 
                                  command=lambda: self.con.dump('seek 30 0 unrated black formula\r\n'))
        self.submenu4.add_command(label='Standard 60 0', 
                                  command=lambda: self.con.dump('seek 60 0 unrated black formula\r\n'))

        self.menu1.add_separator()

        self.menu1.add_command(label='Set Rating Range', command=self.set_rating_range)

        self.menu1.add_separator()
        self.menu1.add_command(label='Specific Seek', command=self.find)
        self.menubar.add_cascade(label='Seek', menu=self.menu1)
        self.menu2 = Menu(self.menubar, tearoff = 0)
        self.menu2.add_command(label='Shouts', command=self.open_shouts_channel)
        self.menu2.add_command(label='Private Message', command=self.open_private_message)
        self.menu2.add_command(label='Channel', command=self.open_channel_message)
        self.menubar.add_cascade(label='Utils', menu=self.menu2)
        self.menu3 = Menu(self.menubar, tearoff = 0)
        self.menu3.add_command(label='Examine Game', command=self.examine_game)
        self.menu3.add_command(label='Examine User Game', command=self.examine_user_game)
        self.menu3.add_command(label='Unexamine Game', command=self.unexamine_game)
        self.menu3.add_separator()
        self.menu3.add_command(label='Observe Game', command=self.observe_game)
        self.menu3.add_command(label='Unobserve Game', command=self.unobserve_game)
        self.menubar.add_cascade(label='Tools', menu=self.menu3)

        self.menu4 = Menu(self.menubar, tearoff = 0)
        self.menu4.add_command(label='Connect', command=self.plug)
        self.menu4.add_separator()
        self.menu4.add_command(label='Quit', command=lambda: self.con.dump('quit\r\n'))
        self.menubar.add_cascade(label='Server', menu=self.menu4)

        self.menu5 = Menu(self.menubar, tearoff = 0)
        self.menu5.add_command(label='White Best Move', command=self.white_best_move)
        self.menu5.add_command(label='Black Best Move', command=self.black_best_move)
        self.menu5.add_separator()
        self.menu5.add_command(label='Play Best Move', command=self.play_best_move)
        self.menu5.add_separator()
        self.menu5.add_command(label='Engine Setup', command=self.setup_engine)
        self.menubar.add_cascade(label='Engine', menu=self.menu5)


        # This frame will contain the board.
        # I use relief=RAISED to give a cuter look and feel.
        self.frame1 = Frame(self, border=3, padx=5, pady=5, relief=RAISED)

        # It initializes board and pass self.frame1 as instance
        # it means it will stay inside frame1.
        self.board = Board(self.frame1, send_move=lambda data: self.con.dump(data))

        # We want it to expand.
        self.board.pack(expand=True)

        self.menu6 = Menu(self.menubar, tearoff = 0)
        self.menu6.add_command(label='Inc Width  <Shift-Right>', command=self.board.incwidth)
        self.menu6.add_command(label='Inc Height <Shift-Up>', command=self.board.incheight)
        self.menu6.add_separator()
        self.menu6.add_command(label='Dec Width <Shift-Left>', command=self.board.decwidth)
        self.menu6.add_command(label='Dec Height <Shift-Down>', command=self.board.decheight)
        self.menu6.add_separator()
        self.menu6.add_command(label='Inc Shape <F1>', command=self.board.incshape)
        self.menu6.add_command(label='Dec Shapet <F2>', command=self.board.decshape)
        self.menubar.add_cascade(label='Appearence', menu=self.menu6)


        
        # We pack it to the left and make it fill in both
        # directions.
        self.frame1.pack(side='left', expand=True, fill=BOTH)

        # We bind key press events to the board functions
        # incheight, decheight, incwidth, decwidth 
        # these functions are responsible by increasing
        # the board size.
        self.bind('<Shift-Up>', lambda widget: self.board.incheight())
        self.bind('<Shift-Down>', lambda widget: self.board.decheight())

        self.bind('<Shift-Right>', lambda widget: self.board.incwidth())
        self.bind('<Shift-Left>', lambda widget: self.board.decwidth())

        # This frame will contain most widgets.
        self.frame2 = Frame(self)
        
        # We instantiate our clock as though it were a frame.
        # Since it inherits from Frame class.
        self.clock = Clock(self.frame2, border=3, relief=RAISED)

        self.clock.pack(side='top', fill=BOTH)

        # This frame contains options relative to game.
        self.frame3 = Frame(self.frame2, border=3, relief=RAISED, padx=5, pady=5)

        self.img1 = PhotoImage(file=rsc('icon', 'take-one.gif'))
        self.img2 = PhotoImage(file=rsc('icon', 'take-two.gif'))
        self.img3 = PhotoImage(file=rsc('icon', 'draw.gif'))
        self.img4 = PhotoImage(file=rsc('icon', 'resign.gif'))
        self.img5 = PhotoImage(file=rsc('icon', 'abort.gif'))
        
        # Whenever one clicks on it it sends a takeback 1
        # to fics.
        self.button1 = Button(self.frame3, image=self.img1, 
                              command = lambda :self.con.dump('takeback 1\r\n'))

        # It asks for take back two moves.
        self.button2 = Button(self.frame3, image=self.img2, 
                              command = lambda :self.con.dump('takeback 2\r\n'))

        # It suggests draw to your opponent.
        self.button3 = Button(self.frame3, image=self.img3, 
                              command = lambda :self.con.dump('draw\r\n'))

        
        # You just resign.
        self.button4 = Button(self.frame3, image=self.img4,
                              command = lambda :self.con.dump('resign\r\n'))
        
        # You got in a bad position maybe it is time to abort
        # it sends request for aborting the game.
        self.button5 = Button(self.frame3, image=self.img5, 
                              command = lambda :self.con.dump('abort\r\n'))

        self.button1.pack(side='left', expand=True, fill=BOTH)
        self.button2.pack(side='left', expand=True, fill=BOTH)
        self.button3.pack(side='left', expand=True, fill=BOTH)
        self.button4.pack(side='left', expand=True, fill=BOTH)
        self.button5.pack(side='left', expand=True, fill=BOTH)
        self.frame3.pack(fill=X)

        # This frame contains buttons that are useful when examining games.
        self.frame4 = Frame(self.frame2, padx=5, pady=5, border=3, relief=RAISED)
        self.img6 = PhotoImage(file=rsc('icon', 'start.gif'))
        self.img7 = PhotoImage(file=rsc('icon', 'back.gif'))
        self.img8 = PhotoImage(file=rsc('icon', 'foward.gif'))
        self.img9 = PhotoImage(file=rsc('icon', 'end.gif'))

        # It goes back to the beginning of a game.
        self.button6 = Button(self.frame4, text='Start', image=self.img6,     
                              command = lambda :self.con.dump('backward 999\r\n'))

        # It goes back one move.
        self.button7  = Button(self.frame4, text='Back', image=self.img7,
                               command = lambda :self.con.dump('backward\r\n'))

        # It goes ahead one move.
        self.button8    = Button(self.frame4, text='Go', image=self.img8, 
                                 command = lambda : self.con.dump('forward\r\n'))

        # It goes to the end.
        self.button9   = Button(self.frame4, text='End', image=self.img9, 
                                command = lambda :self.con.dump('forward 999\r\n'))

        self.button6.pack(side='left', expand=True, fill=X) 
        self.button7.pack(side='left', expand=True, fill=X) 
        self.button8.pack(side='left', expand=True, fill=X) 
        self.button9.pack(side='left', expand=True, fill=X) 
        self.frame4.pack(side='bottom',  fill=X)

        # This frame contains the console widgets.
        self.frame5 = Frame(self.frame2)

        # It contains the text console widget.
        self.frame6 = Frame(master=self.frame5,  border=3,  relief=RAISED,  
                            padx=5,  pady=5)

        # It contains the entry console widget.
        self.frame7 = Frame(master=self.frame5,  border=3,  relief=RAISED,  
                            padx=5,  pady=5)

        self.scrollbar = Scrollbar(master=self.frame6)
        self.scrollbar.pack(side='right', fill=Y)

        # We dump all what comes in this text widget.
        self.text = Text(self.frame6, background='black',foreground='green',width=60, 
                         yscrollcommand=self.scrollbar.set, font=('Helvetica', 8))

        self.scrollbar.config(command=self.text.yview)

        # We use it to send commands.
        self.entry = Entry(self.frame7)
        self.entry.pack(fill=X)

        self.frame7.pack(side='bottom', fill=X)
        self.frame6.pack(side='top', expand=True, fill=BOTH)
        self.text.pack(side='top', expand=True, fill=Y)

        # Whenever you press enter it dumps the command.
        self.entry.bind('<KeyPress-Return>', self.send_cmd)
        self.frame5.pack(side='top', expand=True, fill=Y)
        self.frame2.pack(side='right', fill=Y)

        self.bind('<Shift-Up>', lambda widget: self.board.incheight())
        self.bind('<Shift-Down>', lambda widget: self.board.decheight())
        self.bind('<Shift-Right>', lambda widget: self.board.incwidth())
        self.bind('<Shift-Left>', lambda widget: self.board.decwidth())
        self.bind('<F1>', lambda widget: self.board.incshape())
        self.bind('<F2>', lambda widget: self.board.decshape()) 

        # We initialize the process of reading from the socket.
        # It basically tells untwisted core to start listening for
        # reading, writting processes.
        self.screen()

    def screen(self):
        # We use 200ms that's a good number.
        self.after(200, self.screen)

        # We will not block on select. So, we dont want to wait.
        core.gear.timeout = 0

        # It tells untwisted to listen for reading, writting on the
        # sockets.
        core.gear.update()

    def ask_address(self):
        host = askstring('Server', 'Type the server address.', initialvalue='freechess.org')
        if not host: return

        port = askinteger('Port', 'Type the port.', initialvalue=5000)
        if not port: return

        return host, int(port)

    def plug(self):
        # If it is connected then we have to unplug.
        if self.con: self.unplug()
        host, port = self.ask_address()         

        # We create our connection socket.
        sock = socket(AF_INET, SOCK_STREAM)

        # It wraps the socket so we can install protocols into it.
        self.con = Spin(sock)
        
        Client(self.con)
       
        # It maps CONNECT to send_ident.
        self.con.add_map(CONNECT, self.send_ident) 
        self.con.add_map(CONNECT_ERR, lambda con, err: self.update_text(con, 'Connection failed.'))
        self.con.add_map(CONNECT_ERR, lambda con, err: lose(con))

        self.con.connect_ex((host, port))

    @coroutine
    def send_ident(self, con):
        # Basic untwisted protocols required by fics protocol.
        Stdin(self.con)
        Stdout(self.con)
        Terminator(self.con, '\n\r')
        # Finally we install fics protocol.
        fics.install(self.con)
        
        # If it happens of the server closing
        # the connection then we just close the socket
        # and destroy it.
        self.con.add_map(CLOSE, lambda con, err: lose(con))

         # Whenever it comes data we print it on our console.
        self.con.add_map(Terminator.FOUND, self.update_text)

        # The '<12>' is an event issued by fics protocol
        # it means you are either playing a game or examining a
        # game. In both case we need to update the state of the
        # board.
        self.con.add_map('<12>', self.update_state)

        # It waits for the user sending login
        # when the session starts we can send style 12.

        self.username, = yield con, fics.START_SESSION
        self.con.dump('set style 12\r\n')

    def play_best_move(self):
        fen = fenstring(self.last_state)
        self.stockfish.send('%s\n' % fen)
        self.stockfish.send('go depth %s\n' % self.stockfish_depth)
        once(self.stockfish, stockfish.BESTMOVE, lambda expect, move: self.con.dump('%s\r\n' % move))

    def black_last_move_score(self):
        pass

    def white_last_move_score(self):
        pass

    def unplug(self):
        self.con.dump('quit\r\n')
        self.con = None    

    def find(self):
        seek   = Seek(self)
        option = seek()
        data   = 'seek %s %s %s %s formula %s-%s\r\n' % option

        self.con.dump(data)

    def send_cmd(self, widget):
        data = self.entry.get()
        self.text.insert(END, '%s\n' % data)
        self.text.yview(MOVETO, 1.0)
        self.entry.delete(0, END)
        self.con.dump('%s\r\n' % data)

    def update_text(self, con, data):
        self.text.insert(END, '%s\n' % data)
        self.text.yview(MOVETO, 1.0)
        
    def update_state(self, con, *args):
        """ 
        Whenever '<12>' is issued this function
        is called with the respective arguments.
        """

        position   = args[:8]
        white_time = int(args[-9])
        black_time = int(args[-8])
        white      = args[16]
        black      = args[17]
        flip       = int(args[-3])
        turn       = args[8]
        
        # We update the title so we can know
        # which game we are playing.
        self.title('%s x %s' % (white, black))
        self.clock.click(turn, white_time, black_time)

        # It tells the board to update the position based
        # on the flip.
        self.board.update_position(position, flip)

        # It is interesting to have if you want to analyze games
        # with some engine.
        self.last_state = args

    def setup_engine(self):
        self.stockfish_depth = askstring('Stockfish Depth', 'Depth:', initialvalue=self.stockfish_depth)
        self.stockfish_path = askstring('Engine Path', 'Engine Path:', initialvalue=self.stockfish_path)

        setting = shelve.open(os.path.join(os.path.expanduser('~'), '.snz'))
        setting['depth'] = self.stockfish_depth
        setting['path'] = self.stockfish_path
        setting.close()

    def set_rating_range(self):
        AskRating(self, self.con)
        
    def examine_game(self):
        num = askstring('Examine', 'Game Number:', initialvalue='')
        self.con.dump('examine %s %s\r\n' % (self.username, num))

    def examine_user_game(self):
        nick = askstring('Examine', 'Nick:', initialvalue='')
        num = askstring('Examine', 'Game Number:', initialvalue='')
        self.con.dump('examine %s %s\r\n' % (nick, num))

    def unexamine_game(self):
        self.con.dump('unexamine\r\n')

    def observe_game(self):
        num = askstring('Examine', 'Game Number/Nick', initialvalue='')
        self.con.dump('observe %s\r\n' % num)

    def unobserve_game(self):
        num = askstring('Examine', 'Game Number/Nick', initialvalue='')
        self.con.dump('unobserve %s\r\n' % num)

    def open_shouts_channel(self):
        chat = Chat(self, lambda data: self.con.dump('shout %s\r\n' % data), 'Shouts', self.username)
        self.con.add_map(fics.SHOUT, lambda con, nick, mode, msg: chat.update_text('%s shouts%s %s' % (nick, mode, msg)))

        # i must umap after the window is destroyed.
        # chat.protocol('WM_DELETE_WINDOW', self.cancel)

    def open_private_message(self):
        nick = askstring('Nick', 'Nick:')
        chat = Chat(self, lambda data: self.con.dump('tell %s %s\r\n' % (nick, data)), nick, self.username)

        self.con.add_map('%s tells you:' % nick, lambda con, mode, msg: chat.update_text('<%s>%s' % (nick, msg)))
        self.con.add_map('%s says:' % nick, lambda con, mode, msg: chat.update_text('<%s>%s' % (nick, msg)))

    def white_best_move(self):
        last_state    = list(self.last_state)
        last_state[8] = 'W'
        fen = fenstring(self.last_state)
        self.stockfish.send('%s\n' % fen)
        self.stockfish.send('go depth %s\n' % self.stockfish_depth)
        once(self.stockfish, stockfish.BESTMOVE, lambda expect, move: showinfo('White best move in the position', move))

    def black_best_move(self):
        last_state    = list(self.last_state)
        last_state[8] = 'B'
        fen = fenstring(self.last_state)
        self.stockfish.send('%s\n' % fen)
        self.stockfish.send('go depth %s\n' % self.stockfish_depth)
        once(self.stockfish, stockfish.BESTMOVE, lambda expect, move: showinfo('Black best move in the position', move))

    def open_channel_message(self):
        pass

    def view_statistics(self):
        pass

    def view_user_statistics(self):
        pass

    def view_history(self):
        pass

    def view_user_history(self):
        pass

    def find(self):
        seek = Seek(self)
        option = seek()
        data = 'seek %s %s %s %s formula %s-%s\r\n' % option
        self.con.dump(data)
Ejemplo n.º 33
0
    die()


if __name__ == '__main__':
    import sys
    fd = open(sys.argv[1], 'r')
    code = fd.read()
    fd.close()

    ADDR = 'codepad.org'
    PORT = 80

    HEADER = {
        'Host': ADDR,
        'User-Agent': "uxreq/1.0",
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
        'Connection': 'close',
    }

    PARAMS = {'code': code, 'lang': 'Plain Text', 'submit': 'Submit'}

    con = Spin()

    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, post_data('/', data=PARAMS, header=HEADER))
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    core.gear.mainloop()
Ejemplo n.º 34
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()
Ejemplo n.º 35
0
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()







Ejemplo n.º 36
0
    ADDR = 'codepad.org'    
    PORT = 80

    HEADER = {
    'Host':ADDR,
    'User-Agent':"uxreq/1.0", 
    'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
    'Connection':'close',
    }


    PARAMS = {
                    'code':code,
                    'lang':'Plain Text',
                    'submit':'Submit'
               }

    con  = Spin()

    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, post_data('/', data=PARAMS, header=HEADER))
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    core.gear.mainloop()



Ejemplo n.º 37
0
 def __init__(self, spin, wrap = lambda sock: Spin(sock)):
     spin.add_map(READ, self.update)
     self.wrap = wrap
Ejemplo n.º 38
0
def connect(servaddr, port, nick, user, nick_passwd, adm_passwd, chan_list,
            plugmap):
    sock = socket(AF_INET, SOCK_STREAM)
    ip = gethostbyname(servaddr)
    server = Spin(sock)
    Client(server)

    def auto_join(server, *args):
        send_cmd(server, nick_passwd)
        for ind in chan_list:
            send_cmd(server, 'JOIN %s' % ind)

    @coroutine
    def get_myaddr(server, servaddr, nick, msg):
        server.nick = nick
        send_cmd(server, 'USERHOST %s' % nick)
        args = yield server, '302'
        _, _, ident = args
        user, myaddr = ident.split('@')
        server.myaddr = myaddr

    def update_nick(server, nick_x, user, host, nick_y):
        if server.nick == nick_x:
            server.nick = nick_y

    def handle_connect(server):
        Stdin(server)
        Stdout(server)
        Terminator(server)

        Irc(server)
        CTCP(server)

        Misc(server)
        adm.install(server)

        xmap(
            server, 'PING', lambda server, prefix, servaddr: send_cmd(
                server, 'PONG :%s' % servaddr))

        xmap(server, CLOSE, lambda server, err: lose(server))
        logcon(server)

        xmap(server, '376', auto_join)
        xmap(server, '376', get_myaddr)
        xmap(server, 'NICK', update_nick)
        plugmap(server)

        server.servaddr = servaddr
        server.port = port
        server.nick = nick
        server.user = user
        server.chan_list = chan_list
        server.nick_passwd = nick_passwd
        server.adm_passwd = adm_passwd
        server.plugmap = plugmap

        send_cmd(server, 'NICK %s' % nick)
        send_cmd(server, 'USER %s' % user)

    xmap(server, CONNECT, handle_connect)
    xmap(server, CONNECT_ERR, lambda server, err: lose(server))

    server.connect_ex((ip, port))
    return server
Ejemplo n.º 39
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()