def start(self, mode): """ In CLIENT_MODE: Establish a connection with the server before sending a new message. In SERVER_MODE: Start listening to the incoming messages from the clients. :param mode: (int) Either CLIENT_MODE or SERVER_MODE. :return: None """ address = 'tcp://{}:{}'.format(self.ip, self.port) if mode == CLIENT_MODE: self.mode = mode self.logger.info( "Starting Scoreboard Client listening on {}:{} ...".format( self.ip, self.port)) self.instance = Proxy(address) elif mode == SERVER_MODE: self.mode = mode self.scoreboard = Scoreboard() server = Server(self, address) self.logger.info( "Starting Scoreboard Server listening on {}:{} ...".format( self.ip, self.port)) server.serve_forever()
def main(args=None): import argparse parser = argparse.ArgumentParser('Starts an server') parser.add_argument( '-g', '--gui', action='store_true', help='Open a small window to display the server status.') parser.add_argument('-v', '--verbose', action='store_true', help='Print debug information to the console.') parser.add_argument('-p', '--path', type=str, help='Append this path to sys.path') parser.add_argument('rep_endpoint', help='REP endpoint of the Server') parser.add_argument('pub_endpoint', help='PUB endpoint of the Server') args = parser.parse_args(args) if args.path: sys.path.append(args.path) if args.verbose: LOGGER.addHandler(logging.StreamHandler()) LOGGER.setLevel(logging.DEBUG) from pizco import Server s = Server(None, args.rep_endpoint, args.pub_endpoint) print('Server started at {}'.format(s.rep_endpoint)) if args.gui: if sys.version_info < (3, 0): from Tkinter import Tk, Label else: from tkinter import Tk, Label import time while s.served_object is None: time.sleep(.1) name = s.served_object.__class__.__name__ root = Tk() root.title('Pizco Server: {}'.format(name)) Label(root, text='{}'.format(name)).pack(padx=5) Label(root, text='REP: {}'.format(s.rep_endpoint)).pack(padx=5) Label(root, text='PUB: {}'.format(s.pub_endpoint)).pack(padx=5) root.resizable(width=False, height=False) root.mainloop() else: print('Press CTRL+c to stop ...') s.serve_forever() print('Server stopped')
# -*- coding: utf-8 -*- from pizco import Server from common import House s = Server(House(), 'tcp://127.0.0.1:8000') s.serve_forever()