Sep-30-2021, 12:22 PM
Hi,
I'm trying to learn how to achieve a server/client communication.
So, as a client, I'm injecting a code 'manually' and reading the server's response, but I just cannot get it right.
I appreciate some guidance.
Expected results:
I'm trying to learn how to achieve a server/client communication.
So, as a client, I'm injecting a code 'manually' and reading the server's response, but I just cannot get it right.
I appreciate some guidance.
Expected results:
Connected to: 192.168.1.5 data b'password mypass' info: password mypass list ['password', 'mypass'] cmd password val mypass data b'update 3' info: update 3 list ['update', '3'] cmd update val 3 Pin 29: level=0 Pin 29: level=1 192.168.1.5 Disconnected...Actual results:
Connected to: 192.168.1.5 data b'password mypass' info: password mypass list ['password', 'mypass'] cmd password val mypass data b'update, 3' info: update, 3 list ['update,', '3'] cmd update, val 3 # ... missing relay Pin feedback ... data b'' 192.168.1.14 Disconnected...SERVER FUNCTION CODE
def run(self): global BUFFER_SIZE auth = False sendResponse(self.socket, 'sendcode', '') while 1: data = self.socket.recv(BUFFER_SIZE) print('data',data) if not data: break info = data.decode('UTF-8') print('info: ', info) list = info.split() print('list',list) cmd = list[0] print('cmd', cmd) val = list[1] print('val',val) if cmd == 'password': if val == PASSWORD: auth = True sendResponse(self.socket, 'password', getInputs()) connectionsList.append(self.socket) else: sendResponse(self.socket, 'password', 'codenotok') self.socket.shutdown(0) if auth: if cmd == 'update': try: index = int(val) if 0 <= index < MAX_OUTPUT_PINS: threading.Thread(target=flipOutput, args=(self.socket, index)).start() except ValueError as ex: try: print('Can not convert value to index.') finally: ex = None del ex if cmd == 'get' and val == 'status': sendResponse(self.socket, 'update', status) if self.socket in connectionsList: connectionsList.remove(self.socket) print(self.ip + ' Disconnected...')CLIENT CODE
import socket import time HOST = '192.168.1.130' PORT = 10000 clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: clientSocket.connect((HOST, PORT)) except Exception as e: print("Cannot connect to the server:", e) print("Connected") data = 'password mypass' # Send data to server clientSocket.send(data.encode()) # Receive data from server dataFromServer = clientSocket.recv(64) # Print to the console status = dataFromServer.decode() print(status) # turn on relay 1 data = 'update, 1' # Send data to server clientSocket.send(data.encode()) # Receive data from server dataFromServer = clientSocket.recv(64) # Print to the console status = dataFromServer.decode() print(status) time.sleep(1) # <---- CODE STOPS HERE ----> # turn on relay 2 data = 'update, 2' # Send data to server clientSocket.send(data.encode()) # Receive data from server dataFromServer = clientSocket.recv(64) # Print to the console status = dataFromServer.decode() print(status) clientSocket.close()PYCHARM CONSOLE:
C:\Python39\python.exe C:/SharedFiles/Python/practice/test.py Connected {"ctx": "sendcode", "status": ""} {"ctx": "password", "status": [1, 1, 1, 1, 1, 1, 1, 1]} Process finished with exit code 0