]> AND Private Git Repository - Cipher_code.git/blob - measure_energy_iot/server_tcp4.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
new
[Cipher_code.git] / measure_energy_iot / server_tcp4.py
1 import socket
2 import os
3
4 import buffer
5
6 HOST = 'bilbo'
7 PORT = 2345
8
9 # If server and client run in same local directory,
10 # need a separate place to store the uploads.
11 try:
12     os.mkdir('uploads')
13 except FileExistsError:
14     pass
15
16 s = socket.socket()
17 s.bind((HOST, PORT))
18 s.listen(10)
19 print("Waiting for a connection.....")
20
21 while True:
22     conn, addr = s.accept()
23     print("Got a connection from ", addr)
24     connbuf = buffer.Buffer(conn)
25
26     while True:
27
28
29         file_name = connbuf.get_utf8()
30         if not file_name:
31             break
32 #        file_name = os.path.join('uploads',file_name)
33 #        print('file name: ', file_name)
34
35         file_size = int(connbuf.get_utf8())
36         print('file size: ', file_size )
37
38         with open("/tmp/"+file_name, 'wb') as f:
39             remaining = file_size
40             while remaining:
41                 chunk_size = 4096 if remaining >= 4096 else remaining
42                 chunk = connbuf.get_bytes(chunk_size)
43                 if not chunk: break
44                 f.write(chunk)
45                 remaining -= len(chunk)
46             if remaining:
47                 print('File incomplete.  Missing',remaining,'bytes.')
48             else:
49                 print('File received successfully.')
50     print('Connection closed.')
51     conn.close()
52