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

How do you send .txt over TCP/IP line by line with a certain delay?

我的代码:

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,因为根据我发现的情况,连接已中断,以防我设法正确实施 pickle。

服务器错误:

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 解码为服务器端的可读格式。 pickle.loads 只使用一个参数,所以我不能指定任何编码,例如 utf-8。

.txt 文件导出自 Excel(制表符间距):

Data.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

谢谢。

由于您想通过网络传输 pandas 系列,实现此目的的一种方法是使用 pickle。 pickle 可以接受一个 Series 并将其转换为字节数组,然后在另一个方向上将字节数组转换为一个系列。

在客户端,使用:

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)