Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. ---------------------SERVER-----------------
  2. import socket, os
  3. from threading import Thread
  4. from time import sleep as s
  5.  
  6. # TODO
  7. # na balo support me multiple client support    ------ MALON LEITOURGEI IDFK
  8.  
  9. # kanoume mia socket metabliti kai pernoume tin topothesia arxeiou kai to megethos tou arxeiou
  10. socketObject = socket.socket()
  11. fileLocation = str(raw_input("File location:  "))
  12. file = open(fileLocation, "rb")
  13. fileSize = os.path.getsize(fileLocation)
  14.  
  15. # kanoume bind tin ip tou host kai to port
  16. socketObject.bind(("192.168.10.9", 50000))
  17.  
  18.  
  19. # kanoume accept to request tou client kai tou stelnoume to filesize
  20. def acceptinClients(welcomeMessage):
  21.     while True:
  22.         print(welcomeMessage)
  23.         socketObject.listen(1)
  24.         client, addr = socketObject.accept()
  25.         client.send(str(fileSize))
  26.         print("Client {} connected").format(addr)
  27.         Thread(target=sendingFiles(client)).start()
  28.  
  29.  
  30. # edo diabazoume to file kai to stelnoume ston client pou ekane connect kai otan teliosei kleinei to connection
  31. def sendingFiles(onomaClient):
  32.     fileInformation = file.read(fileSize)
  33.     onomaClient.send(fileInformation)
  34.     while fileInformation:
  35.         fileInformation = file.read(fileSize)
  36.         onomaClient.send(fileInformation)
  37.     onomaClient.close()
  38.  
  39.  
  40. Thread(target=acceptinClients("Transfering file....")).start()
  41.  
  42. ----------------------CLIENT------------------------
  43. import socket
  44.  
  45. # metablites tou socket kai tin ip tou host
  46. socketObject = socket.socket()
  47. hostIP = str(raw_input("Host's ip:  "))
  48.  
  49. # prospathoume na kanoume ena connection ston host kai na paroume to arxeio kai to megethos tou
  50. socketObject.connect((hostIP, 50000))
  51. receivedFileSave = open("C:\Users\Sakis\Downloads\ReceivedFile", "wb")
  52. fileSize = int(socketObject.recv(1024))
  53.  
  54. # pernoume to file kai to grafoume sto download folder mas
  55. receivedFile = socketObject.recv(fileSize)
  56. while receivedFile:
  57.     receivedFileSave.write(receivedFile)
  58.     receivedFile = socketObject.recv(fileSize)
  59.  
  60. # klinoume to file kai to socket metabliti
  61. receivedFileSave.close()
  62. socketObject.close()
  63.  
  64. print("Received File fron Host")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement