0

以确定大小的块将字节从客户端流式传输到服务器的最佳方式是什么?

现在我正在用 base64 编码一个音频文件,然后用 zlib 压缩它并通过套接字连接发送。我的问题是尝试在服务器中重建原始文件。

我使用一个空字符串进行了思考和测试,该字符串与服务器正在接收的所有字节一起添加。看起来不错,但是开头的“b'”被保留了,导致它无法恢复原始音频文件。

我刚刚尝试解码字节并从服务器接收到的每组字符串的开头删除“b'”和从结尾(data[2:-1])删除“”,但这减少了一些原著中的人物。

客户端:

with open(arquivo, 'rb') as input_file:
   abc = base64.b64encode(input_file.read())

try1 = zlib.compress(abc)

n = 338
result = [try1[i:i+n] for i in range(0, len(try1), n)]

HOST = ''
PORT = 9999

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((HOST,PORT))

i = 0

for item in result:
    item = str(item)
    print(item)
    s.send(item.encode())
    i += 1
    print('i = ', i)
    time.sleep(2)

服务器端:

HOST = ''
PORT = 9999

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

print('Servidor Inicializado')
s.bind((HOST,PORT))
s.listen()
audiofile = ''
i = 0
conn, addr = s.accept()



while True:


    data1 = conn.recv(2048)
    print('data1 undecoded = ', data1)
    text = data1.decode()

    data = text[2:-1]
    print('data EDITADO = ', data)

    audiofile = audiofile + data
    i += 1
    print('i = ', i)
    print('audiofile = ', audiofile)

    if not data:
        print('No Data Received!')

    print('Recebeu tratado :', data)

不知道如何进行,任何帮助表示赞赏。谢谢!

4

1 回答 1

1

这是我如何使用套接字发送和接收数据的示例。

通常我会腌制它们。如果您不熟悉 pickle,它用于序列化 python 对象以存储或通过诸如套接字之类的连接发送。

客户端:

import pickle

with open(arquivo, 'rb') as input_file:
   abc = base64.b64encode(input_file.read())
# I haven't used these libraries so I'm assuming you know how to unpack it from here
try1 = zlib.compress(abc)

HOST = ''
PORT = 9999

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((HOST,PORT))

# serialize the python object 
message = pickle.dumps(try1)

# get the length of the pickled object
length = len(message)
# convert into a fixed width string
length = str(length).rjust(8, '0')
# send the length of the object we will send
s.sendall(bytes(length, 'utf-8'))

# send the object
self.client.sendall(message)

服务器端:

import pickle

HOST = ''
PORT = 9999

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind((HOST,PORT))
s.listen()

conn, addr = s.accept()

# get the length of the object we are about to receive
length = conn.recv(8)
# turn it back into an int
length = int(length.decode('utf-8'))

# I keep this to determine if we've received everything or not
full_length = length
message = None

# loop until we've zeroed out our length
while length > 0:
    # only receive what we need
    # at a maximum of 128 bit chunks
    chunk_len = min(128, length)

    length -= chunk_len

    chunk = conn.recv(chunk_len)
    if message is None:
        message = chunk
    else:
        message = message + chunk

# Edit: I've had issues with slow connections not receiving the full data
# for those cases adding something like this works
while len(message) < full_length:
    chunk_len = min(128, full_length - len(message))
    chunk = conn.recv(chunk_len)
    message = message + chunk

# now that we've received everything, we turn it back into a python object
try1 = pickle.loads(message)

# this should be the same try1 you sent

免责声明:我没有对此进行任何测试,也不知道 try1 对象是什么或您想用它做什么。这只是从a点到b点。

于 2020-01-16T18:19:10.227 回答