Python program for file sharing using socket programming (client-server file sharing)
File: server.py
import socket,os

server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0)
server_socket.bind(("192.168.122.1",5000))
server_socket.listen(5)
print("TCP server waiting for client on port 5000")

while 1:
    client_socket,address=server_socket.accept()
    print("Connection established at",address)
    name=input("Enter text-file name to send ")
    path=os.path.join(os.getcwd(),name)
    file1=open(path,'rb')
    data=file1.read(1024)
    while data:
        client_socket.send(data)
        data=file1.read(1024)
        client_socket.close()
    print("File sent!")
    break;
File: client.py
import socket
client_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM,0)
client_socket.connect(("192.168.122.1",5000))
while 1:
    data=client_socket.recv(1024)
    while data:
        print(data)
        data=client_socket.recv(1024)
        client_socket.close()
    print("\nFile received!")
    break;
Output (Terminal-1)
kodingwindow@kw:~$ hostname -i | awk '{print $3}'
192.168.43.44

kodingwindow@kw:~$ python3 server.py
TCP server waiting for client on port 5000
Connection established at ('192.168.43.44', 47058)
Enter text-file name to send kw.py
File sent!
Output (Terminal-2)
kodingwindow@kw:~$ python3 client.py
b'print("Hello, World!")\n'

File received!
Advertisement