收到的数据不正确

时间:2017-06-02 19:41:47

标签: 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 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()

0 个答案:

没有答案