Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------SERVER-----------------
- import socket, os
- from threading import Thread
- from time import sleep as s
- # TODO
- # na balo support me multiple client support ------ MALON LEITOURGEI IDFK
- # kanoume mia socket metabliti kai pernoume tin topothesia arxeiou kai to megethos tou arxeiou
- socketObject = socket.socket()
- fileLocation = str(raw_input("File location: "))
- file = open(fileLocation, "rb")
- fileSize = os.path.getsize(fileLocation)
- # kanoume bind tin ip tou host kai to port
- socketObject.bind(("192.168.10.9", 50000))
- # kanoume accept to request tou client kai tou stelnoume to filesize
- def acceptinClients(welcomeMessage):
- while True:
- print(welcomeMessage)
- socketObject.listen(1)
- client, addr = socketObject.accept()
- client.send(str(fileSize))
- print("Client {} connected").format(addr)
- Thread(target=sendingFiles(client)).start()
- # edo diabazoume to file kai to stelnoume ston client pou ekane connect kai otan teliosei kleinei to connection
- def sendingFiles(onomaClient):
- fileInformation = file.read(fileSize)
- onomaClient.send(fileInformation)
- while fileInformation:
- fileInformation = file.read(fileSize)
- onomaClient.send(fileInformation)
- onomaClient.close()
- Thread(target=acceptinClients("Transfering file....")).start()
- ----------------------CLIENT------------------------
- import socket
- # metablites tou socket kai tin ip tou host
- socketObject = socket.socket()
- hostIP = str(raw_input("Host's ip: "))
- # prospathoume na kanoume ena connection ston host kai na paroume to arxeio kai to megethos tou
- socketObject.connect((hostIP, 50000))
- receivedFileSave = open("C:\Users\Sakis\Downloads\ReceivedFile", "wb")
- fileSize = int(socketObject.recv(1024))
- # pernoume to file kai to grafoume sto download folder mas
- receivedFile = socketObject.recv(fileSize)
- while receivedFile:
- receivedFileSave.write(receivedFile)
- receivedFile = socketObject.recv(fileSize)
- # klinoume to file kai to socket metabliti
- receivedFileSave.close()
- socketObject.close()
- print("Received File fron Host")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement