1

I want to my client program to send objects to server. By searching the internet, in all the examples, I have seen data objects are sent by the server to the client, but I want to do the opposite. Below I am sharing both the client codes and the server code. I have been able to modify the socket client code, but I am having difficulty in modifying the server code. As, I am new to python programming and I am on a deadline, a help is greatly appreciated.

Here are the codes:

Server code::

import socket



def server_program():

    # get the hostname
    host = socket.gethostname()
    port = 5000  # initiate port no above 1024
    server_socket = socket.socket()  # get instance
    # look closely. The bind() function takes tuple as argument
    server_socket.bind((host, port))  # bind host address and port together

    # configure how many client the server can listen simultaneously
    server_socket.listen(2)
    conn, address = server_socket.accept()  # accept new connection
    print("Connection from: " + str(address))
    while True:
        # receive data stream. it won't accept data packet greater than 1024 bytes
        data = conn.recv(1024).decode()
        if not data:
            # if data is not received break
            break
        print("from connected user: " + str(data))
        data = input(' -> ')
        conn.send(data.encode())  # send data to the client

    conn.close()  # close the connection


if __name__ == '__main__':
    server_program()

Here is the client code modified with pickle::

import socket
import pickle


def client_program():
    host = socket.gethostname()  # as both code is running on same pc
    port = 5000  # socket server port number

    client_socket = socket.socket()  # instantiate
    client_socket.connect((host, port))  # connect to the server

    message = input(" -> ")  # take input

    while message.lower().strip() != 'bye':
        client_socket.send(message.encode())  # send message
        #data = client_socket.recv(1024).decode()  # receive response

        #print('Received from server: ' + data)  # show in terminal

        wastebin_attr = {1:"Wastebin_serial_number", 2:"Wastebin_type", 3:"Status" , 4:"Action", 5:"Zone", 6:"Latitude", 7:"Longitude"}
        mssg = pickle.dumps(wastebin_attr)
        print("from connected user: " + mssg)
        message = input(" -> ")  # again take input

    client_socket.close()  # close the connection


if __name__ == '__main__':
    client_program()

1 Answer 1

1

To send data from the client to the server using the pickle module:

data = pickle.dumps(data)
client_socket.send(data)

To receive the data on the servers end use:

data = conn.recv(1024)
data = pickle.loads(data)

You could also use this to send from server to client by switching the client_socket and conn parts

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. I am getting somewhere with it. Just instead of 'data' I am using 'mssg'. But there is another issue that have arise. On compilation I am getting the following error: Sorry: IndentationError: unexpected indent (Test_server.py, line 23)
Python is picky on where the tabs go. Everything must be tabbed and spaced correctly otherwise it will not run.
Yes, you are right, I did some research and I found it has something to do with spacings and tabs. I shall look into it. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.