def main(): # open and start the server and close afterwards print( "This program shows all important features that are possible with the pynetworking library.\n\n" ) with net.ClientManager(server_address, ClientCommunicator): # Connect a client to the server ServerCommunicator.connect(server_address, blocking=True, timeout=2) # create an alias for all server functions (Not necessary, but code looks better) server = ServerCommunicator.remote_functions # Call functions at the server print( f"{'='*7} Calling a function with arguments and return value {'=' * 7}\n" ) types = server.get_types(1, "He", [1, 2], main, lambda a, b: a + b) print(types) print( f"\n\n{'=' * 7} Calling a function that raises an error {'=' * 7}\n" ) try: server.risky_function() except NotImplementedError as e: print(e) print("Error successfully transmitted") print( f"\n\n{'=' * 7} Calling a function that calls a function at the client {'=' * 7}\n" ) server.start_communication() # Close the connection to the server at client-side ServerCommunicator.close_connection()
def greet_client(name: str): print(f"Hello {name}") ret_value = net.ClientManager().get().remote_functions.client_func("Paul") if ret_value: print("Cool you responded") else: print("Mhhh something went wrong ") return "Goodbye"
def main(): # open and start the server and close afterwards # As arguments, the address, to which the server listens to and the class that overrides the ClientCommunicator is # needed with net.ClientManager(server_address, ClientCommunicator): # Connect a client to the server # The connection is stored statically. This means no object needs to be created and passed to other functions. # Once the connection is established, you can call the functions from everywhere ServerCommunicator.connect(server_address, blocking=True, timeout=2) # create an alias for all server functions (Not necessary, but code looks better) server = ServerCommunicator.remote_functions while True: message = input(">>>") server.recv_message_from_client(message) if message == "bye": break # Close the connection to the server at client-side ServerCommunicator.close_connection()
def recv_message_from_client(message: str) -> None: print(f"Client: {message}") if message == "bye": client: ClientCommunicator = net.ClientManager().get() clients_last_words = client.remote_functions.get_last_words() print(clients_last_words)
def notify_other_devices(user: '******'): clients: Dict[int, net_interface.ClientCommunicator] = net.ClientManager().clients for client in clients.values(): if client.user_id == user.user_id and client.device_id != user.device_id: client_logger_sync().info(f"Trigger server synchronization: client={client}") client.remote_functions.trigger_server_synchronization()
def get_client() -> ClientCommunicator: """Main purpose is to supply better auto completions""" return net.ClientManager().get()
def start_communication(): client: ClientCommunicator = net.ClientManager().get() client.remote_functions.a_client_function("Hi from Server!")
:synopsis: All functions located at the client_side """ import pynetworking as net import Interface def client_func(name) -> bool: print(f"Hello {name}") return True def client_faculty(number: int) -> int: if number <= 1: return number return number * Interface.ServerCommunicator.remote_functions.server_faculty( number - 1) if __name__ == '__main__': address = ("127.0.0.1", 5000) with net.ClientManager(address, Interface.ClientCommunicator): Interface.ServerCommunicator.connect(address) ret_value = Interface.ServerCommunicator.remote_functions.server_faculty( 5) print(f"Client: {ret_value}") ret_value = Interface.ServerCommunicator.remote_functions.greet_client( "Walter") print(f"Client: {ret_value}") Interface.ServerCommunicator.close_connection()
def request_login(): username = net.ClientManager().get().remote_functions.get_username() password = net.ClientManager().get().remote_functions.get_password() return is_valid_data(username, password)
import interface import pynetworking as net import time def request_login(): username = net.ClientManager().get().remote_functions.get_username() password = net.ClientManager().get().remote_functions.get_password() return is_valid_data(username, password) def is_valid_data(username, password): # This function just simulates a possible database access and validation if len(username) > 4 and len(password) > 4: return True return False if __name__ == '__main__': address = ("127.0.0.1", 5000) client_manager = net.ClientManager(address, interface.ClientCommunicator) client_manager.start() time.sleep(20) client_manager.stop_listening() client_manager.stop_connections()
def server_faculty(number: int) -> int: if number <= 1: return number return number * net.ClientManager().get().remote_functions.client_faculty( number - 1)
def many_func_in_func(): return net.ClientManager().get().remote_functions.client_faculty(5)
def func_in_func(start: int) -> bool: ret = net.ClientManager().get().remote_functions.incrementer(start) return ret + 1
def return_client_id() -> int: return net.ClientManager().get().id