-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
36 lines (32 loc) · 1.26 KB
/
Copy pathclient.py
File metadata and controls
36 lines (32 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import socket
import sys
class FileSender:
def __init__(self, socket, file):
self.socket = socket
self.file = file
def send_file(self):
try:
self.socket.sendall(f"{self.file}\n".encode()) # filename = os.path.basename(self.file)
with open(self.file, "rb") as open_file:
open_file.seek(0, 2)
taille = open_file.tell() # size = os.path.getsize(self.file)
socket_client.sendall(f"{str(taille)}\n".encode())
open_file.seek(0)
part = open_file.read(1024)
while part:
self.socket.sendall(part)
part = open_file.read(1024)
except FileNotFoundError as e:
print(f"Le fichier {self.file} est introuvable.")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_client:
server_ip = str(sys.argv[1])
server_port = int(sys.argv[2])
server_addr = (server_ip, server_port)
socket_client.connect(server_addr)
while True:
doc = input("Entrez le chemin du fichier à envoyer:")
if doc == "quit":
break
else:
envoi = FileSender(socket_client, doc)
envoi.send_file()