File size: 1,913 Bytes
feb8a3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This file will be used for recieving files over socket connection.
import os
import socket
import time
import streamlit as st
# import streamlit as st
# import time

progress_text = "File Transferring...Please Wait"



from PIL import Image

image = Image.open('img2.jpg')

st.image(image)

st.title('Reciver Page')
st.divider()
st.header("Share files easily, securely, and fast!")
host = st.text_input("Host Name: ")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Trying to connect to socket.



with st.spinner('Searching For Sender...'):
    time.sleep(3)
    try:
        sock.connect((host, 40000))
        st.warning("Connected Successfully...",icon="✔")
    except:
        st.warning("Unable to connect",icon="⚠️")
        exit(0)

# Send file details.
file_name = sock.recv(100).decode()

file_size = sock.recv(100).decode()

# Opening and reading file.
with open("./rec/" + file_name, "wb") as file:
    c = 0
    my_bar = st.progress(0, text=progress_text)
    st.write("Waiting for Sender to select the File")
    # Starting the time capture.
    start_time = time.time()
    
    # Running the loop while file is recieved.
    while c <= int(file_size):
        # my_bar.progress(50, text=progress_text)
        
        data = sock.recv(1024)
        if not (data):
            break
        file.write(data)
        
        c += len(data)
        

    # Ending the time capture.
    end_time = time.time()
for percent_complete in range(100):
    time.sleep(0.1)
    my_bar.progress(percent_complete + 1, text=progress_text)
# my_bar.progress(100, text=progress_text)
with open("./rec/" + file_name,"rb") as file:
    btn = st.download_button(
            label="Download File",
            data=file,
            file_name=file_name,
            
          )
st.write("File transfer Complete.Total time: ", int(end_time - start_time))

# Closing the socket.
sock.close()