Search code examples
pythonsocketsarabicvalueerrornon-english

ValueError when transmitting a file titled in Arabic over a Python tcp socket


I'm building a socket program to transmit files between two computers. Files with English titles are transmitted successfully, but when I try to send Arabic titled files (eg وثيقة.docx), I get a long list of ValueErrors, starting with:

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

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

My code is: Server:

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()

Client:

import

 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)

What is the problem here? Please help.


Solution

  • You send the size in unicode character of the title, and then try to send it encoded in utf-8. For example with your example:

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

    gives

    10 15
    

    The peer will only use 10 bytes for the file name, and will use the remaining 5 as the beginning of the file size. And will choke for .docx not being the beginning of a binary number. What happens...

    Fix is easy, build the byte string before computing its length:

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