'Goin into loop in server-client structure

I am trying to set up a server-client structure in python but after a few data transaction(client and server send a message, than client send a big message and waits for a result), program going into loop. When i don't use while loop, it doesn't send big files. But when i use while loop, it doesn't stop. How can i solve this?

(i suppose the loop part is while True: data = conn.recv(4096) in server.py)

client.py:

import socket                # Import socket module

s = socket.socket()             # Create a socket object
host = socket.gethostname()
host='192.168.56.1'     # Get local machine name
port =3030                 # Reserve a port for your service.

s.connect((host, port))
supp=700000
s.send(str(supp).encode("utf-8"))

print(s.recv(1024).decode())


# with open('DATABASE_863144x42.pckl', 'rb') as f:# i've tried this but in big 
# files this doesn't work
#     to_send=f.read()
#     s.send(to_send)
#     print("File has sent")
   
   

filename='python1.pdf'
f = open(filename,'rb')
l = f.read()
print('read')
while (l):
   s.send(l)
   # print('Sent ',repr(l))
   l = f.read()
f.close()
print('closed')

     
result = s.recv(1024)  
print('result taken') 
with open('result.txt', 'wb') as f:
   
   print("data arrived")
   f.write(result)   

print('Successfully get the file')
s.close()
print('connection closed')

server.py :


import socket                   # Import socket module

port = 3030                     # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
s.bind((host, port))            # Bind to the port
s.listen(5)                     # Now wait for client connection.

print ('Server listening....')

def resultf(supp):
   return 'x'

while True:
   conn, addr = s.accept()   
   print ('Got connection from', addr)
   
   supp = conn.recv(1024)
   print('Server received', supp.decode('utf-8'))
   
   conn.send(supp)
   
   with open('mytext.txt', 'wb') as f:
       # data = conn.recv(96000000)# i've tried this but in big 
       # files this doesn't work
       # print("data arrived")
       # f.write(data)
               
       while True:
           data = conn.recv(4096)
           if not data:
               break
           f.write(data)
       print('a')
       
           
   result=resultf(int(supp))     
   conn.send(result.encode())
           
   print('Done sending')
   conn.close()



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source