-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoutonReception.py
More file actions
60 lines (51 loc) · 1.71 KB
/
BoutonReception.py
File metadata and controls
60 lines (51 loc) · 1.71 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import multiprocessing
from PyQt5.QtCore import QThread
from Bouton import Bouton
import socket
import time
import os
class BoutonReception(Bouton):
def __init__(self,parent,position,label):
super().__init__(parent,position,label)
self.parent = parent
self.clicked.connect(self.recevoir)
def preRecep(self):
server_addr = "0.0.0.0"
server_port = 5001
BUFFER_SIZE = 4096
SEPARATOR = "||||||"
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((server_addr,server_port))
s.listen(5)
client_socket, address = s.accept() #Le client est connecte
received = client_socket.recv(BUFFER_SIZE).decode()
filename ,filesiz = received.split(SEPARATOR)
filename = os.path.basename(filename)
filesize = int(filesiz)
self.parent.pbar.setRange(0,int(filesize/1024))
with open(filename, "wb") as f:
inc= 0
while True:
lecture = client_socket.recv(BUFFER_SIZE)
if not lecture:
print("transfert terminé")
break
f.write(lecture)
inc+=int(BUFFER_SIZE/1024)
self.parent.pbar.setValue(inc)
self.parent.pbar.setValue(int(filesize/1024))
client_socket.close()
s.close()
time.sleep(1.5)
self.parent.message.click()
def recevoir(self):
t = MyThread(target=self.preRecep())
t.start()
class MyThread(QThread):
def __init__(self, target=None):
super().__init__()
self.target = target
def run(self):
if self.target:
self.target()