如何将字典数据从客户端发送到python服务器,以便与使用socket编程的df2进行比较?

2024-05-29 01:31:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我面临一个套接字编程的问题。这是一个服务器端python程序,我在其中从客户端接收到字典格式的df1数据。我怎样才能从df1和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()

客户-侧边.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!!')

我收到的输出是:-

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.

Tags: nameimportdataserverissysargsplace
2条回答

最好使用cmp来比较字典。我添加了比较两本词典的代码。你知道吗

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() 

您正在比较dictstr,这就是比较失败的原因。你知道吗

也许,pickle将数据发送给服务器,并在服务器上进行适当的解码。你知道吗

在这种情况下,使用JSON进行编码和解码是合适的。你知道吗

比如,在服务器端

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()
...

在客户端

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

相关问题 更多 >

    热门问题