-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.py
51 lines (28 loc) · 831 Bytes
/
sender.py
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
import socket
import os
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))
s.listen(5)
server_ip = socket.gethostbyname(host)
print("\nListening to ip : ",server_ip)
print('Waiting for connection...')
conn, addr = s.accept()
print(f"\nConnection established with {addr[0]} : {addr[1]}\n")
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
filename = filedialog.askopenfilename()
print("Selected file:", filename)
file_size = os.path.getsize(filename)
conn.sendall(file_size.to_bytes(8, byteorder='big'))
with open(filename, 'rb') as f:
while True:
data = f.read(1024)
if not data:
break
conn.sendall(data)
print('File sent successfully\n\n')
conn.close()