Python 接收到错误数据

Python 接收到错误数据,python,list,split,server,client,Python,List,Split,Server,Client,我正在用Python编写一个客户端和服务器应用程序,收到的数据有问题。在第一个“循环”中,我收到了好数据,但在下一个“循环”中,我收到了坏数据。我该怎么办?也许你有一个更好的主意来发送和接收数据 这是客户: import socket import pickle import sys host = socket.gethostname() port = 2004 BUFFER_SIZE = 100000 print("What you want to do:\r1. Select from

我正在用Python编写一个客户端和服务器应用程序,收到的数据有问题。在第一个“循环”中,我收到了好数据,但在下一个“循环”中,我收到了坏数据。我该怎么办?也许你有一个更好的主意来发送和接收数据

这是客户:

import socket
import pickle
import sys

host = socket.gethostname() 
port = 2004
BUFFER_SIZE = 100000 
print("What you want to do:\r1. Select from base\r2.Insert into base")
MESSAGE, MESSAGE1 = input("tcpClientA: Enter message/ Enter   exit:").split(",")
tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpClientA.connect((host, port))
while MESSAGE != 'exit':
    tcpClientA.send(MESSAGE.encode())
    tcpClientA.send(MESSAGE1.encode())
    lista=pickle.loads(tcpClientA.recv(BUFFER_SIZE).strip())
    print(lista)
    print("Variables 1 and 2 are: ", MESSAGE, MESSAGE1)
    MESSAGE, MESSAGE1 = input("tcpClientA: Enter message to continue/     Enter exit:").split(",")    

tcpClientA.close()
这是服务器:

import socket 
from threading import Thread
import pyodbc
import pickle

# Multithreaded Python server : TCP Server Socket Thread Pool
class ClientThread(Thread): 

    def __init__(self,ip,port): 
        Thread.__init__(self) 
        self.ip = ip 
        self.port = port 
        print ("[+] New server socket thread started for " + ip + ":" + str(port) )

    def run(self): 
        while True :           
            connsql = pyodbc.connect('DRIVER={SQL Server};SERVER=I7-KOMPUTER\SQLEXPRESS;DATABASE=test')
            cursor = connsql.cursor()            
            data = conn.recv(2048)

            #datasplitx, datasplity = data.decode().split(",", 1)  
            try:
                xy = []
                xy = data.decode().split(" ")
                print("dat: ",data.decode())
                print("After del:",xy)
                x=str(xy[0])
                #y=str(xy[1])
                #x, y = [str(x) for x in data.decode().split()]  
                #y=str(datasplit[1])
                #x = str(datasplit[0])
                #y = str(datasplit[1])

            except ValueError:
                print("List does not contain value")
            print ("Server received data:", xy)
            if x == 'exit':
                break
            if x == '1':
            #if data.decode() == '1':
                cursor.execute("select rtrim(name) from client")
                rows = cursor.fetchall()
                zap=pickle.dumps(rows)
                conn.send(zap)
                print(pickle.loads(zap))

            if x == '2':    
            #if data.decode() == '2':             
                cursor.execute("insert into dbo.klient values('"+y+"')")
                connsql.commit()
                zro=pickle.dumps("Done.")
                conn.send(zro)
            del xy[:]
            print ("cleared list: xy",xy)

# Multithreaded Python server : TCP Server Socket Program Stub
TCP_IP = '0.0.0.0' 
TCP_PORT = 2004 
BUFFER_SIZE = 1024  # Usually 1024, but we need quick response 

tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
tcpServer.bind((TCP_IP, TCP_PORT)) 
threads = []

while True: 
    tcpServer.listen(4) 
    print ("Multithreaded Python server : Waiting for connections from TCP clients...\r" )
    (conn, (ip,port)) = tcpServer.accept() 
    newthread = ClientThread(ip,port) 
    newthread.start() 
    threads.append(newthread) 
    print("Client IP: " +str(ip))

for t in threads:
t.join()

你收到了什么样的坏数据?预期的行为是什么?我想输入两个变量并发送它们。服务器将接收数据,现在我希望拆分为两个变量,并用于选择、插入等。在运行应用程序后的第一次输入“1,数据字符串”服务器接收到两个变量,下次我输入相同的变量,我在列表“1”中接收到其他变量。我不知道第二个变量在哪里。我有解决问题的方法:tcpClientA.send(MESSAGE.encode())tcpClientA.send(MESSAGE1.encode())to data=MESSAGE+MESSAGE1 tcpClientA.send(data.encode())