GRAPHIC: New Entity Operations™ Alpha Logo

EntityScript



core_server




"""
Copyright (C) 2020 New Entity Operations Inc.

ALL RIGHTS RESERVED

core_server manages your personal server instance

"""
# Imports: Standard
from socket import (
    AF_INET, SOCK_STREAM, socket
)
from threading import (
    current_thread, Thread
)
from socketserver import (
    BaseRequestHandler, ThreadingMixIn, TCPServer
)

from core_middlelayer import (
    encoding_standard,
    SLUG_HOST_PERSONAL, STREAM_VALUE,
    PORTCONNECT_TO
)
# SILO.silo_writer
class SILO:
    """
    Takes an appended THREAD_response from
    STANDARD_HANDLER_TCP_REQUEST.handle
    """
    silo_writer = []

class STANDARD_HANDLER_TCP_REQUEST(BaseRequestHandler):
    """
    STANDARD HANDLER: TCP
    Serve a control instance on port X until killed
    with Ctrl-C or automated as an up/down routine elsewhere
    """
    def handle(self):
     """
     the 'handle(self)' method is a required override.
     we want ascii type requests
     """
     # self.request is the TCP socket
     try:
      data = str(self.request.recv(int(STREAM_VALUE)),
       encoding_standard
      )
      THREAD_now = current_thread()
      THREAD_response = bytes(
       "{}: {}".format(THREAD_now.name, data),
       encoding_standard
      )
      self.request.sendall(THREAD_response)
      print(THREAD_response)
      SILO.silo_writer.append(THREAD_response)
     except:
      UnicodeDecodeError
      print(
       "It's possible you aren't enabling unsigned local certs or there isn't one there."
      )

class STANDARD_HANDLER_TCP_SERVER(
 ThreadingMixIn, TCPServer
):
    """
    Acts as the allocated of each thread.
    'pass' by default as a converted thread object
    """
    pass

def EVENT_INBOUND(ip, port, message):
    """
    ip, port, message style I/O on a socket
    AF_INET and SOCK_STREAM are the protocols in use
    """
    with socket(AF_INET, SOCK_STREAM) as sock:
     try:
      sock.connect((ip, port))
      sock.sendall(bytes(message, encoding_standard))
      THREAD_response = str(sock.recv(STREAM_VALUE), encoding_standard)
      print("Received: {}".format(THREAD_response))
     except:
      ConnectionRefusedError
      print("Connect Error: Possibly no server?")

if __name__ == '__main__':
    # HOST, PORT = "localhost", 19288
    # 0 for an arbitrary unused port
    HOST, PORT = SLUG_HOST_PERSONAL, int(PORTCONNECT_TO)

    try:
     server = \
     STANDARD_HANDLER_TCP_SERVER(
     (HOST, PORT),
     STANDARD_HANDLER_TCP_REQUEST
     )
    except:
     OSError
     pass

    try:
     with server:
      try:
       #***********************************#
       # STEP 7: SERVER Gateway CHECK         #
       #***********************************#
       print(
        "-------- START: STEP 7 - SERVER Gateway CHECK ----------"
       )

       ip, port = server.server_address

       THREAD_server  = Thread(
        target=server.serve_forever
       )
       THREAD_server.daemon = True

       THREAD_server.start()

       print("THREADED SERVER: Up",
        THREAD_server.name
       )

       #EVENT_INBOUND(ip, port, "Hello my friend")
       #EVENT_INBOUND(ip, port, "Hello my friend")
       #EVENT_INBOUND(ip, port, "Hello my foe")
       #EVENT_INBOUND(ip, port, "Hello my All")

       server.serve_forever()
       print(
        "---------- STOP: STEP 7 - SERVER Gateway CHECK ---------\n-"
       )
       #***************************************#
       # STOP: STEP  7 - SERVER Gateway CHECK   #
       #***************************************#

      except:
       KeyboardInterrupt
       print(
        "\n\nThe server was shutdown by the Keyboard-Interrupt: Exiting\n"
       )
       # Write the contents of connections here. Add them to silos to keep track and kill off
       # any that may become unwanted.
       for i in SILO.silo_writer:
           print(i)

      #server.shutdown()

    except:
     #******************************************#
     # START: STEP  7 - SERVER Gateway CHECK      #
     #******************************************#
     NameError
     print(
      "-------- START: STEP  7 - SERVER Gateway CHECK  ----------"
     )
     print(
      "The server-socket is already running."
     )
     print(
      "---------- STOP: STEP  7 - SERVER Gateway CHECK  ----------\n"
     )
     #***************************************#
     # STOP: STEP  7 - SERVER Gateway CHECK  #
     #***************************************#



Return HOME