如何以一定的延迟通过 TCP/IP 逐行发送.txt?



我的代码:

Client.py

import socket 
import pandas as pd 
import time
import pickle
# Load data
File = pd.read_csv('Data.txt', sep='t', skipinitialspace=True, header=None, skiprows=1)
client_socket = socket.socket()
client_socket.connect(('localhost', 5555))  # Connect to server
client_socket.send(bytes("Sending file...", 'utf-8'))  # Note
Buffer = []
for x in range(File.shape[0]):
count = 1
Buffer = File.loc[count]
client_socket.send(pickle.dumps(Buffer))
count += 1
time.sleep(2)  # Wait 2 sec

这是我的服务器:

Server.py

import socket
import pickle 
server_socket = socket.socket()  
server_socket.bind(('localhost', 5555))  # local server-client
server_socket.listen(3) # Max. 3 connections
print('Waiting for connection')
Buffer = []
i = 0
while True:  
client_socket, addr = server_socket.accept()  
received_data = client_socket.recv(1024).decode()
print('Client address', addr, received_data)
client_socket.recv(pickle.loads(Buffer)).decode()
print(Buffer[i])
i =+ 1
if(i == 10000):
client_socket.close()  # Closing server socket
else: continue

我遇到了以下错误:

BrokenPipeError: [Errno 32] Broken pipe

问题出在SIGPIPE,因为根据我的发现,连接会中断,以防我设法正确实现泡菜。

服务器错误:

Traceback (most recent call last):
File "/Users/David/PycharmProjects/DP1/Server.py", line 19, in <module>
client_socket.recv(pickle.loads(Buffer))
TypeError: a bytes-like object is required, not 'list'

我希望decode()服务器一侧将收到的泡菜以字节格式解码为可读格式。pickle.loads只使用一个参数,所以我不能指定任何编码,例如 utf-8。

.txt文件是从 Excel 导出的(制表符间距(:

数据.txt

Time    Speed   R_Ax    Activation  Delay   KP
11:11:37    13,1    124,45  100 2   4
11:11:39    13,08   124,26  100 2   4
11:11:41    13,15   124,925 100 2   4
11:11:43    13,08   124,26  100 2   4
11:11:45    13,11   124,545 100 2   4
11:11:47    13,13   124,735 100 2   4
11:11:49    13,13   124,735 100 2   4
11:11:51    13,05   123,975 100 2   4
11:11:53    13,07   124,165 100 2   4
11:11:55    13,11   124,545 0   2   999
11:11:57    13,1    124,45  0   2   999
11:11:59    13,06   124,07  0   2   999
11:12:01    13,07   124,165 0   2   999
11:12:03    12,99   123,405 0   2   999
11:12:05    13,03   123,785 0   2   999
11:12:07    13,05   123,975 0   2   999
11:12:09    13,11   124,545 0   2   999
11:12:11    13,04   123,88  0   2   999
11:12:13    13,04   123,88  0   2   999

谢谢。

由于您想通过线路传输熊猫系列,因此实现此目的的一种方法是使用泡菜。 pickle 可以获取一个系列并将其转换为字节数组,然后转到另一个方向并将字节数组转换为系列。

在客户端,使用:

client_socket.send(pickle.dumps(Buffer))

然后,在服务器端使用:pickle.loads

详细解答

双方的实际代码如下所示:

客户:

import socket
import pandas as pd
import time
import pickle
# Load data
File = pd.read_csv('Data.txt', sep='t', skipinitialspace=True, header=None, skiprows=1)
client_socket = socket.socket()
client_socket.connect(('localhost', 5555))  # Connect to server
client_socket.send(bytes("Sending file...", 'utf-8'))  # Note
Buffer = []
for inx in range(File.shape[0]):
Buffer = File.loc[inx]
print(f"Sending n {Buffer}")
client_socket.send(pickle.dumps(Buffer))
time.sleep(2)  # Wait 2 sec

服务器:

import socket
import pickle
server_socket = socket.socket()
server_socket.bind(('localhost', 5555))  # local server-client
server_socket.listen(3) # Max. 3 connections
print('Waiting for connection')
Buffer = []
i = 0
client_socket, addr = server_socket.accept()
received_data = client_socket.recv(1024)
print('Client address', addr, received_data)
while True:
received_data = client_socket.recv(10000)
Buffer.append(pickle.loads(received_data))
print(Buffer[i])
i =+ 1
client_socket.close()  # Closing server socket (we'll never get here) 

最新更新