-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.py
More file actions
28 lines (22 loc) · 715 Bytes
/
sender.py
File metadata and controls
28 lines (22 loc) · 715 Bytes
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
import socket
import time
import json
import struct
import os
def send_file(conn: socket.socket, localfilepath, sdfsfileid, timestamp):
header_dic = {
'sdfsfileid': sdfsfileid, # 1.txt
'timestamp': timestamp,
'file_size': os.path.getsize(localfilepath)
}
header_json = json.dumps(header_dic)
header_bytes = header_json.encode()
conn.send(struct.pack('i', len(header_bytes)))
conn.send(header_bytes)
with open(localfilepath, 'rb') as f:
for line in f:
conn.send(line)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('127.0.0.1', 10086))
timestamp = time.time()
send_file(s, 't.pdf', 'aaa', timestamp)