简体   繁体   中英

How can I send dictionary data from client side to python server for comparison with df2 using socket programming?

I am facing an issue with socket programming. This is a server-side python program where I received df1 data from the client-side which is in dictionary format. How can I receive the same data from both sides that is df1 and df2?

import socket
import sys 


def server_program():
    HOST = '192.168.0.115' 
    PORT = 8888

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    print('socket created')

    try:
        s.bind((HOST, PORT))
    except socket.error as err:
        print('Bind Failed, Error Code: ' + str(err[0]) + ', Message: ' + err[1])
        sys.exit()

    print('Socket Bind Success!')

    s.listen(10)
    print ('Socket is now listening')


    while True:
        conn, addr = s.accept()
        print ('Connect with ' + addr[0] + ':' + str(addr[1]))
        df1 = conn.recv(1024)
        print(df1)
        conn.close()
        break
    s.close()


    df2 = {"name":"rajat", "place":"rajasthan"} # here I put df2 equals to the df1 to get compared.
    print(df2)

    if df1 == df2:
        print('yes Data Exists')
    else:
        print('No Data')

if __name__ == '__main__':
    server_program()

client-side.py

import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.0.115', 8888)) #IP is the server IP

for args in sys.argv:
    if args == '':
        args = 'no args'
    else:
        df1 = {"name":"rajat", "place":"rajasthan"}
        s.sendall(df1)
print('Data is sent to the server!!')

The output which I am receiving is:-

C:\Users\Pallavai\Desktop>python ese.py
socket created
Socket Bind Success!
Socket is now listening
Connect with 192.168.0.115:56105
{"name":"rajat", "place":"rajasthan"}
{'name': 'rajat', 'place': 'rajasthan'}
No Data

Why these both are not same as df2 is directly printed.

You are comparing a dict with a str , that's why the comparison fails.

Maybe, pickle the data and send it and decode it appropriately on the server.

In this case, using JSON to encode and decode is appropriate.

Like, in the server side,

import json
...
print ('Connect with ' + addr[0] + ':' + str(addr[1]))
df1 = conn.recv(1024)
df1 = json.loads(df1.decode('utf-8')) # decode the dict from JSON
conn.close()
...

And in the client side,

import json
...
df1 = {"name":"rajat", "place":"rajasthan"}
s.sendall(json.dumps(df1).encode('utf-8')) # encode the dict to JSON
...

Better to use cmp to compare dictionary. I have added code for comparing two dictionary.

import socket
import sys 


def server_program():
    HOST = '192.168.0.115' 
    PORT = 8888

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


    print('socket created')

    try:
        s.bind((HOST, PORT))
    except socket.error as err:
        print('Bind Failed, Error Code: ' + str(err[0]) + ', Message: ' + err[1])
        sys.exit()

    print('Socket Bind Success!')

    s.listen(10)
    print ('Socket is now listening')


    while True:
        conn, addr = s.accept()
        print ('Connect with ' + addr[0] + ':' + str(addr[1]))
        df1 = conn.recv(1024)
        print(df1)
        conn.close()
        break
    s.close()


    df2 = {"name":"rajat", "place":"rajasthan"} # here I put df2 equals to the df1 to get compared.
    print(df2)

    res = cmp(df1, df2)
    print(res) # If both are equal, it will print 0 else -1
    if res == 0:
        print('yes Data Exists')
    else:
        print('No Data')

if __name__ == '__main__':
    server_program() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM