I tried to create a client server script for sending and receiving some files. The scenario is that initially Server sends automatically a csv file to client, client process the file and sends back an answer file.

When I start receiving the file I can see all the contents of the file but is not doing a break to continue to the next function for sending back the answer file Is server part problem or client or both?

Client

#GET THE WORK FILE
with open('received_file.csv', 'wb') as f:
    print ('file opened')
    while True:
        data = s.recv(BUFFER_SIZE)
        print('receiving data...')
        print('data=%s', (data))
        #data =''
        if not data:
            print('Successfully get the file')
            break

        f.write(data)
    f.close()

Only if I place data=' ' it goes next but shouldn't server sends an empty data packet? It wouldn't work if a received file is over buffer size correct?

Server

while True:
    print('Starting is ',starting)
    l = f.read(BUFFER_SIZE)
    while (l):
        self.sock.send(l)
        print('Sending..') 
        l = f.read(BUFFER_SIZE)
        if not l:
            f.close()
            starting = 0
            del filenames[0]
            print('Sending in over..')

            self.getfile()

1 Answers

0
Nic Wanavit On

I think the problem is that the break command is exiting the if statement instead of the while loop that you intend to break.

Here is a possible solution

Client

with open('received_file.csv', 'wb') as f:
        print ('file opened')
        while_loop_status=True
        while while_loop_status:
            data = s.recv(BUFFER_SIZE)
            print('receiving data...')
            print('data=%s', (data))
            if not data:
                print('Successfully get the file')
                while_loop_status=False
            f.write(data)
        f.close()

Here you create a variable that determines whether to continue the while loop called "while_loop_status" then you change it from True to False when you need to break the loop.