Guest User

Client.py

a guest
Oct 16th, 2017
2,821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # Code for:
  2. # https://stackoverflow.com/questions/46775320/simple-python-server-client-file-transfer
  3.  
  4. import socket
  5.  
  6. csFT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. csFT.connect((socket.gethostname(), 8756))
  8.  
  9. text_file = 'send.txt'
  10.  
  11. #Send file
  12. with open(text_file, 'rb') as fs:
  13.     #Using with, no file close is necessary,
  14.     #with automatically handles file close
  15.     csFT.send(b'BEGIN')
  16.     while True:
  17.         data = fs.read(1024)
  18.         print('Sending data', data.decode('utf-8'))
  19.         csFT.send(data)
  20.         print('Sent data', data.decode('utf-8'))
  21.         if not data:
  22.             print('Breaking from sending data')
  23.             break
  24.     csFT.send(b'ENDED')
  25.     fs.close()
  26.  
  27. #Receive file
  28. print("Receiving..")
  29. with open(text_file, 'wb') as fw:
  30.     while True:
  31.         data = csFT.recv(1024)
  32.         if not data:
  33.             break
  34.         fw.write(data)
  35.     fw.close()
  36. print("Received..")
  37.  
  38. csFT.close()
Add Comment
Please, Sign In to add comment