ValueError:从客户端向服务器发送数据时的额外数据

时间:2018-01-09 01:46:29

标签: python json python-2.7 json-deserialization

我在python中使用客户端 - 服务器套接字连接,如下所示

here

客户端代码如下所示

   data_tuple = [client_id,client_req_time,type_of_VM_requested,service_time]  # final tuple of data that is sent by client
   data_string = json.dumps(data_tuple)  # data serialized
   clientSocket.send(str(data_string))  # sending data to server

服务器端代码如下

while True:   # extra while is created  so that server keeps running
    running = True
    clientsocket, addr = serversocket.accept()  # accept a connection from client
    print ("\n connected to Client Simulator  at " + ServerIP + " on Port # " + str(Server_port))
    while running:
        receivedData = clientsocket.recv(1024)
        # ------------------ parsing the client request -----------------------#
        if receivedData:
            client_tuple = json.loads(receivedData)  # data de-serialized
            client_id = client_tuple[0]
            arrival_time = str(client_tuple[1])  # from unicode to str
            type_of_vm_requested = str(client_tuple[2])  # from unicode to str
            service_time = str(client_tuple[3])  # from unicode to str

问题是这个程序工作了一段时间,就像在第三个客户端之后出现错误,如下所示

 The server is waiting for Client requests...
 connected to Client Simulator  at 127.0.0.1 on Port # 9988
 Request Received : client id = 1 , Arrival_time = 2018-01-08 20:29:10:093  , Service_time = 12 sec, VM type  = vkey_type_B Looking for a requested VM...  VM is available  VM allocation is done and VM started successfully  Database Allocation update successfully
 Request Received : client id = 2 , Arrival_time = 2018-01-08 20:29:10:859  , Service_time = 8 sec, VM type  = vkey_type_B Looking for a requested VM...  VM is available  VM allocation is done and VM started successfully  Database Allocation update successfully
 Request Received : client id = 3 , Arrival_time = 2018-01-08 20:29:12:394  , Service_time = 24 sec, VM type  = vkey_type_B Looking for a requested VM...  VM is available  VM allocation is done and VM started successfully  Database Allocation update successfully Extra data: line 1 column 50 - line 1 column 294 (char 49 - 293) Traceback (most recent call last):   File "D_pool_logic.py", line 609, in <module>
    run_pool(new_session)                                               # calling the main poolmanager function   File "D_pool_logic.py", line 25, in run_pool
    start_client_request_handling_server(session)  # starts request listening server , as our main server is up and running   File "D_pool_logic.py", line 444, in start_client_request_handling_server
    client_tuple = json.loads(receivedData)  # data de-serialized   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 1 column 50 - line 1 column 294 (char 49 - 293)

客户端的print data_string输出为

[1, "2018-01-08 21:12:01:393", "vkey_type_A", 7]
[2, "2018-01-08 21:12:04:669", "vkey_type_A", 17]
[3, "2018-01-08 21:12:18:777", "vkey_type_B", 11]
[4, "2018-01-08 21:12:30:748", "vkey_type_A", 19]
[5, "2018-01-08 21:12:34:327", "vkey_type_A", 17]
[6, "2018-01-08 21:13:21:729", "vkey_type_A", 18]
[7, "2018-01-08 21:13:29:445", "vkey_type_A", 7]

服务器端print client_tuple的输出是

[1, u'2018-01-08 21:12:01:393', u'vkey_type_A', 7]
[2, u'2018-01-08 21:12:04:669', u'vkey_type_A', 17] 
[3, u'2018-01-08 21:12:18:777', u'vkey_type_B', 11] 

在第3次请求后,它给出了错误

Traceback (most recent call last):
  File "B_VM_creater.py", line 331, in <module>
    start_server(new_session)  # starts request listening server , as our main server is up and running
  File "B_VM_creater.py", line 227, in start_server
    client_tuple = json.loads(receivedData)  # data de-serialized
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 16 - line 1 column 31 (char 15 - 30)

1 个答案:

答案 0 :(得分:0)

在发送数据时使用pickle.dumps()并在接收数据时使用pickle.load()解决了问题

在客户端,代码是

data_tuple = [client_id,client_req_time,type_of_VM_requested,service_time]  # final tuple of data that is sent by client
data_string = pickle.dumps(data_tuple)  # data serialized
clientSocket.send(data_string)  # sending data to server

在服务器端,代码是

if receivedData:
                client_tuple = pickle.loads(receivedData)  # data de-serialized
                client_id = client_tuple[0]
                arrival_time = str(client_tuple[1])  # from unicode to str , as timestamp is a string not a number
                type_of_vm_requested = str(client_tuple[2])  # from unicode to str
                service_time = int(client_tuple[3])  # from unicode to str