Python Forum
Server/client basic communication
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Server/client basic communication
#1
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:
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,480 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,449 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,384 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Client server Multithreading Anan 6 5,837 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,775 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,233 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,441 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,388 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,100 Jun-02-2020, 01:08 PM
Last Post: nuffink
  how can i send a list of tuples from the server to the client using sockets? dafdaf 1 3,887 Apr-13-2020, 10:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020