File size: 1,580 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
# This file is used for sending the file over socket
import os
import socket
import time
import streamlit as st
from PIL import Image

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

st.image(image)

st.title('Sender Page')
st.divider()
st.header("Share files easily, securely, and fast!")

# Creating a socket.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((socket.gethostname(), 40000))
sock.listen(5)
st.write("Host Name: ", sock.getsockname())
st.divider()
with st.spinner('Searching For Receiver...'):
    client, addr = sock.accept()
# Accepting the connection.


# Getting file details.
# file_name = st.text_input("File Name: ","sample_image.png")
file_name = st.selectbox(
    'Select the File',
    ('sample_python.py', 'sample_image.png', 'sample_pdf.pdf',"sample_text.txt"))
st.write("File Selected: ", file_name)
if file_name != "":
    file_size = os.path.getsize(file_name)

    # Sending file_name and detail.
    client.send(file_name.encode())
    client.send(str(file_size).encode())

    # Opening file and sending data.
    with open(file_name, "rb") as file:
        c = 0
        # Starting the time capture.
        start_time = time.time()

        # Running loop while c != file_size.
        while c <= file_size:
            data = file.read(1024)
            if not (data):
                break
            client.sendall(data)
            c += len(data)

        # Ending the time capture.
        end_time = time.time()

    st.write("File Transfer Complete.Total time: ", int(end_time - start_time))
    # Cloasing the socket.
    sock.close()