コード例 #1
0
def main():
    host = "127.0.0.1"
    port = 8001
    backlog = 10

    DOC_ROOT = os.path.abspath(".")
    ERR_DOC = "./webserver/error_document"
    SERVER = "localhost:8001"

    try:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:

            sock.bind((host, port))
            sock.listen(backlog)
            print("クライアントからの接続を待ちます.")

            while True:
                conn, address = sock.accept()
                print("クライアント接続.")

                s_thread = s_t.ServerThread(DOC_ROOT, ERR_DOC, SERVER, conn)
                s_thread.start()

    except KeyboardInterrupt:
        print("\n KeyboardInterrupt. exit.")
コード例 #2
0
ファイル: server.py プロジェクト: nordmoen/IT3105-quarto
 def play_continuous(self):
     self.log.info('Server starting continuous mode')
     self.listener.start()
     try:
         while True:
             if len(self.players) > 1:
                 players = self.players[:]
                 for p in players:
                     if not p.is_alive:
                         players.remove(p)
                         self.players.remove(p)
                         if len(players) == 0:
                             break
                         continue
                     else:
                         self.prepare_player(p)
                 self.log.info('Starting round with %i players',
                               len(players))
                 play_threads = []
                 playing = self.create_random_population(players, 2)
                 self.log.info('Players playing this round: %r', playing)
                 for pair in playing:
                     t = server_thread.ServerThread(pair[0], pair[1])
                     try:
                         pair[0].new_game(1)
                         pair[1].new_game(1)
                     except:
                         self.log.debug('Player is not responding')
                         continue
                     t.start()
                     play_threads.append(t)
                 for thread in play_threads:
                     thread.join()
                     p1 = thread.p1
                     p2 = thread.p2
                     winner, board = thread.get_winner()
                     if not board:
                         #The thread has closed, likely because one of the players
                         #has quit the game
                         continue
                     if not winner:
                         self.ties[p1] += 1
                         self.ties[p2] += 1
                     else:
                         if winner == p1:
                             self.wins[p1] += 1
                             self.loses[p2] += 1
                         else:
                             self.wins[p2] += 1
                             self.loses[p1] += 1
                 self.log.info('Round completed, played %s game(s)',
                               len(playing))
             else:
                 time.sleep(2)
     except:
         self.log.exception(
             'Server threw exception while playing continuously')
     self.shutdown()
     self.print_result()
コード例 #3
0
def start_server():
    '''todo:
    check if the server is alreayd running
    start the http and websocket server in separate thread so
    it is not blocking here
    '''
    global st
    if st != None:
        ghost_log.p("server already started")
        return True

    st = server_thread.ServerThread()
    ret = st.start()
    if not ret:
        st = None
    return ret
コード例 #4
0
ファイル: main.py プロジェクト: mariotan7/WebServerPy
def main():
    host = "127.0.0.1"
    port = 8001
    backlog = 10
    DOCUMENT_ROOT = "./"

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind((host, port))
        sock.listen(backlog)
        print("クライアントからの接続を待ちます.")

        while True:
            conn, address = sock.accept()
            print("クライアント接続.")

            s_thread = server_thread.ServerThread(DOCUMENT_ROOT, conn)
            s_thread.start()
コード例 #5
0
ファイル: server.py プロジェクト: nordmoen/IT3105-quarto
 def play_game(self, num_rounds=1):
     self.log.info('Server starting game mode')
     self.listener.start()
     players_last = len(self.players)
     self.log.info('Waiting for 2 players to connect')
     while len(self.players) < 2:
         time.sleep(2)
         if len(self.players) != players_last:
             self.log.info(
                 'New player connected, we are waiting for %s more player(s)',
                 2 - len(self.players))
             players_last = len(self.players)
         if not self.listener.is_alive():
             self.log.critical('Server listener died! Shutting down')
             return
     self.listener.shutdown()  #We don't need this any more
     self.log.info('Players connected, starting game')
     self.prepare_players()
     p1 = self.players[0]
     p2 = self.players[1]
     for i in range(num_rounds):
         if not p1.is_alive or not p2.is_alive:
             self.log.critical(
                 'One or both of the players has closed their' +
                 ' connection to the game, aborting round. %s, %s', p1, p2)
             break
         self.log.debug('Starting round %i', i + 1)
         try:
             p1.new_game(num_rounds - i)
             p2.new_game(num_rounds - i)
             game = server_thread.ServerThread(p1, p2)
             game.run()
             winningPlayer, board = game.get_winner()
             if game.get_last_exception():
                 self.log.critical('Game threw exception:')
                 self.log.critical(game.get_last_exception())
             if winningPlayer:
                 self.log.debug('Player %s won the game', winningPlayer)
                 self.log.debug('Board:\n %s', board)
                 self.wins[winningPlayer] += 1
                 loser = p1 if winningPlayer == p2 else p2
                 self.loses[loser] += 1
             else:
                 self.log.debug('Players tied the game')
                 self.log.debug('Board:\n %s', board)
                 self.ties[p1] += 1
                 self.ties[p2] += 1
             p1, p2 = p2, p1
             progress = int((float(i + 1) / num_rounds) * 100)
             if progress % 10 == 0:
                 self.log.info('%s%% complete.', progress)
             time.sleep(
                 1)  #Had a problem where receiver did not get the message
             #without this. Can be a local problem
         except:
             self.log.exception('An error occurred while trying to play')
             for p in self.players:
                 p.error()
             break
     self.shutdown()
     self.log.info('Played %i rounds', num_rounds)
     self.print_result()