通过 Python tcp 套接字传输以阿拉伯语命名的文件时出现 ValueError

旱鸭子

我正在构建一个套接字程序来在两台计算机之间传输文件。带有英文标题的文件已成功传输,但是当我尝试发送带有阿拉伯语标题的文件(例如 وثيقة.docx)时,我得到一长串 ValueErrors,开头为:

invalid literal for int() with base 2: b'.docx000000000000000000010000001'

因瓦lid literal for int() with base 2: b'10001PK\x03\x04\x14\x00\x08\x08\x08\x00\xe0'

我的代码是: 服务器:

import socket

serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = 9000
serversock.bind((host,port))
filename = ""
serversock.listen(10)
print("Waiting for a connection.....")

clientsocket, addr = serversock.accept()
print("Got a connection from %s" % str(addr))

while True:
    try:
        size = clientsocket.recv(16) # Note that you limit your filename length to 255 bytes.
        if not size:
            clientsocket, addr = serversock.accept()
            print("Got a connection from %s" % str(addr))
            continue
        size = int(size, 2)
        print('SIZE', size)
        filename = clientsocket.recv(size)
        print('filename', filename)
        filesize = clientsocket.recv(32)
        print('FILESIZE', filesize, 'TYPE', type(filesize))
        filesize = int(filesize, 2) ##########
        file_to_write = open(filename, 'wb')
        chunksize = 4096
        while filesize > 0:
            if filesize < chunksize:
                chunksize = filesize
            data = clientsocket.recv(chunksize)
            file_to_write.write(data)
            filesize -= len(data)

        file_to_write.close()
        print('File (%s) received successfully' % filename.decode('utf-8'))
    except ValueError as verr:
        print(verr)
        #continue
    except FileNotFoundError:
        print('FileNotFoundError')
        #continue
serversock.close()

客户:

进口

 socket
import os
from file_walker import files_to_transmit

def transmit(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((host, port))
        directory = files_to_transmit()
        for file in directory:
            filename = file
            size = len(filename.split('/')[-1]) # split() to get bare file name to transmit to server
            size = bin(size)[2:].zfill(16) # encode filename size as 16 bit binary
            s.send(size.encode('utf-8'))
            s.send(filename.split('/')[-1].encode('utf-8')) # split() to get bare file name

            filename = file
            filesize = os.path.getsize(filename)
            filesize = bin(filesize)[2:].zfill(32) # encode filesize as 32 bit binary
            s.send(filesize.encode('utf-8'))

            file_to_send = open(filename, 'rb')

            l = file_to_send.read()
            s.sendall(l)
            file_to_send.close()
            print('File Sent')

        s.close()
    except ConnectionRefusedError:
        print('ConnectionRefusedError: Server may not be running')

    except ValueError as e:
        print(e)

transmit('localhost', 9000)

这里有什么问题?请帮忙。

塞尔吉·巴列斯塔

您以标题的 unicode 字符发送大小,然后尝试以 utf-8 编码发送它。例如你的例子:

title = 'وثيقة.docx'
print(len(title), len(title.encode('utf8')))

10 15

peer 将只使用 10 个字节作为文件名,并将使用剩余的 5 个字节作为文件大小的开头。并且会因为.docx不是二进制数的开头而窒息怎么了...

修复很容易,在计算其长度之前构建字节字符串:

    ...
    for file in directory:
        filename = file.split('/')[-1].encode('utf_8') # split() to get bare file name
        size = len(filename)

        size = bin(size)[2:].zfill(16) # encode filename size as 16 bit binary
        s.send(size.encode('utf-8'))
        s.send(filename) 
        ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过Python中的TCP套接字发送文件

来自分类Dev

如何通过TCP套接字发送列表-Python

来自分类Dev

在python中导入阿拉伯语Wordnet

来自分类Dev

Python 分类阿拉伯语数据集中的 UnicodeDecodeError

来自分类Dev

无法使用Python将阿拉伯语解码的Unicode保存到CSV文件

来自分类Dev

如何使用 Python 读取包含阿拉伯语行的 CSV 文件

来自分类Dev

通过TCP套接字发送多个文件

来自分类Dev

通过python中的TCP套接字在客户端-服务器之间发送文件?

来自分类Dev

通过python中的TCP套接字在客户端-服务器之间发送文件?

来自分类Dev

Python通过套接字传输文件

来自分类Dev

Java:通过TCP传输文件

来自分类Dev

通过Boost TCP传输文件

来自分类Dev

Java:通过TCP传输文件

来自分类Dev

Python 通过 TCP 读取 modbus

来自分类Dev

通过表单提交阿拉伯语内容导致禁止页面

来自分类Dev

通过TCP套接字实时传输Qt流音频

来自分类Dev

在python 2.7中打印阿拉伯语/波斯语字母

来自分类Dev

python3中波斯语或阿拉伯语的unicode和编码

来自分类Dev

在python 2.7中打印阿拉伯语/波斯语字母

来自分类Dev

通过套接字的Java TCP通信

来自分类Dev

通过TCP构建Unix套接字桥

来自分类Dev

Qt通过TCP发送文件

来自分类Dev

Python中的阿拉伯语Word Net同义词?

来自分类Dev

Python将阿拉伯语存储在数组中吗?

来自分类Dev

python 2.7中阿拉伯语单词的正则表达式

来自分类Dev

使用CopyTo()通过TCP传输文件

来自分类Dev

在Qt中通过TCP传输大文件

来自分类Dev

在Python中通过套接字传输文本文件

来自分类Dev

通过ruby TCP套接字接收大量数据时出现问题