FILE SHARING USING SOCKET PYTHON


Hello What's Up Pyprogrammer Today We will Create a File Sending Application Using Tkinter........

Let's Begin...

We can Share files over Lan Network Using This Awesome program...


Module Required To In This Project:

tkinter-It is used to create GUI applications, it is standard GUI Library. It provide easy method to create Powerful GUI Applications.

socket-This module helps us to connect two nodes on a network to communicate with each other

requests-This module Help us to send Http request using python

os-This module help us to interact with Operating System that is currently running on your system.

random-It is used to generate random numbers..


NOTE: Server and Client are included on same Program.....

Separate programming  is available on our blog...

File Without GUI is also Available.....

CODE GIVEN BELOW...

Application Look





Selecting Files

Sending Files


giving name to received file
Writing IP Address


Message

Output Of Received File



SOURCE CODE:


Download .py File From Below.....


  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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from tkinter import *
import socket
import requests
import os
from tkinter import filedialog
from tkinter import simpledialog
from tkinter import messagebox
import random

root=Tk()
root.geometry("305x600")
root.resizable(False,False)
root.title("PY.SHARE BY sarthak")

frame=Frame(root)
frame.pack()

#Displaying Host Name

host_name=Label(frame,text="HOST NAME: "+socket.gethostname(),font=("Arial", 13,"bold"),bg="lightblue",fg="darkblue")
host_name.pack(ipadx=80,fill=BOTH)

#Displaying Ip Address
ip_name=Label(frame,text="IP ADDRESS: "+socket.gethostbyname(socket.gethostname()),font=("Arial", 13,"bold"),bg="lightblue",fg="darkblue")
ip_name.pack(ipadx=80,fill=BOTH)

#Creating List For File Adding
file_list=Listbox(root,bg="lightgrey",width=20)
file_list.pack(ipadx=90)

#Creating Message Box
note=Label(root,text="Wait Checking Your Connectivity...",bg="black",fg="green",font=("Arial",10,"bold"))
note.pack(ipadx=150)

#Checking Internet Connection

url="https://pyprogrammercode.blogspot.com"
timeout=5
try:
	request = requests.get(url, timeout=timeout)
	note.config(text="Online")
except (requests.ConnectionError, requests.Timeout) as exception:
    note.config(fg="red",text="Offline")
    ip_name.configure(padx=19)   # Only for Looks

#function for adding and removing files

def add():
    global filename
    filename = filedialog.askopenfilename()
    c=str(filename)
    name=os.path.basename(filename)
    if bool(c) is True:
        box.configure(fg="green")
        file_list.insert(END,name)
        box.insert(END,"File Added Succesfully......\n")
    else:
        box.insert(END,"File Not Selected ......\n")

    


    

def remove():
    file_list.delete(ACTIVE)

#File Sharing
def send():

    messagebox.showinfo("Ready To Send....", "Type address or Host name From Reciever Side")
    
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind((socket.gethostname(),1234))
    s.listen(1)
    print(socket.gethostname()) #To Know Socket Name 
    try:
        cli,adr=s.accept()
        box.insert(END,"Waiting For Connection....\n")
        box.insert(END,"Connected Succesfully To: "+str(adr) + "\n")
    except socket.error as error:
        box.insert(END,str(error))
    file_s=filename
    a=os.path.getsize(file_s)
    name,exte=os.path.splitext(file_s)
    b=str(a)
    cli.send(bytes(exte,"utf-8"))  #Sending File  Extension To Client

    print(a)  #Sending File Size To Client
    cli.send(bytes(b,"utf-8"))
    f=open(file_s,"rb")
    data=f.read(a)
    while data:
        cli.send(data)
        data=f.read(a)
        f.close()
        box.insert(END,"File Transfered Succesfully To: " +str(socket.gethostname())+"\n")


#Receving File

def rec():
    ip=simpledialog.askstring("RECIEVER ADDDRESS","ENTER HOST NAME OR IP ADDRESS: ",parent=root)
    
    
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    host=ip
    try:
        s.connect((host,1234))
        box.insert(END,"Succesfully Connected With: "+str(host)+"\n")
    except socket.error as error:
        box.insert(END,str(error))

    directory="PyShare"
    parent_dir="C:/"

    ext=s.recv(10000)
    exte=ext.decode("utf-8")
    print(exte)

    msg=s.recv(100000)
    a=msg.decode("utf-8")
    print(a)
    b=int(a)  
    file_name=simpledialog.askstring("FILENAME","NAME OF RECIEVED FILE:",parent=root)

    if file_name is not None:     
        file_s=str(file_name+str(exte))
        path = os.path.join(parent_dir,directory)
        try:  
            os.mkdir(path)  
        except OSError as error:  
            print("\n")
        save=os.path.join(path,file_s)
        f=open(save,"wb")
        data=s.recv(b,socket.MSG_WAITALL)
        f.write(data)
        f.close()
        box.insert(END,"Your File Is Recieved At: C://PyShare//"+str(file_s)+"\n")
    else:
        name2=str(random.randrange(1,50641206))
        file_s=str(name2+str(exte))
        path = os.path.join(parent_dir,directory)
        try:  
            os.mkdir(path)  
        except OSError as error:  
            print("\n")
        save=os.path.join(path,file_s)
        f=open(save,"wb")
        data=s.recv(b,socket.MSG_WAITALL)
        f.write(data)
        f.close()
        box.insert(END,"Your File Is Recieved At: C://PyShare//"+str(file_s)+"\n")







#frame for Button
frame1=Frame(root,bg="lightgrey")
frame1.pack()

#Creating Button For Adding and Removing Files
add=Button(frame1,text="ADD FILES",font=("Arial",10,"bold"),bg="lightgreen",command=add)
add.grid(row=4,padx=5,ipadx=35,ipady=30)

remove=Button(frame1,text="REMOVE FILE",font=("Arial",10,"bold"),bg="#fa8e70",command=remove)
remove.grid(row=4,column=2,ipadx=15,ipady=30)

#Creating Send Button
send=Button(frame1,text="SEND",font=("Arial",10,"bold"),bg="lightblue",command=send)
send.grid(row=5,padx=5,ipadx=52,ipady=30)

#Creating Recieve Button
rec=Button(frame1,text="RECIEVE",font=("Arial",10,"bold"),bg="yellow",command=rec)
rec.grid(row=5,column=2,padx=5,ipadx=32,ipady=30)

#creating Message Box
box=Text(root,font=("Arial"),height=10,width=50,bg="black",fg="green")
box.pack()

root.mainloop()