write a program on sharing file between 2 computer
To share a file between two computers, you can use a simple client-server model. Below is an example using Python with sockets. This program allows one computer (the server) to send a file to another computer (the client).
### Server Code (server.py)
This code will run on the computer that you want to act as the server (the one sending the file).
```python
import socket
import os
def send_file(conn, filename):
if os.path.isfile(filename):
conn.sendall(f"{filename}\n".encode()) # Send filename
with open(filename, 'rb') as f:
while True:
bytes_read = f.read(1024)
if not bytes_read:
break
conn.sendall(bytes_read) # Send the file data
print("File sent successfully.")
else:
print("File does not exist.")
def main():
host = '0.0.0.0' # Listen on all interfaces
port = 5001 # Port to listen on
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen(1)
print("Waiting for a connection...")
conn, addr = server_socket.accept()
print(f"Connection from {addr}")
filename = input("Enter the filename to send: ")
send_file(conn, filename)
conn.close()
server_socket.close()
if __name__ == "__main__":
main()
```
### Client Code (client.py)
This code will run on the computer that you want to act as the client (the one receiving the file).
```python
import socket
def receive_file(conn):
filename = conn.recv(1024).decode().strip() # Receive the filename
if not filename:
return
with open(f"received_{filename}", 'wb') as f:
print(f"Receiving {filename}...")
while True:
bytes_read = conn.recv(1024)
if not bytes_read:
break
f.write(bytes_read) # Write the file data
print("File received successfully.")
def main():
host = input("Enter server IP address: ")
port = 5001 # Port to connect to
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
receive_file(client_socket)
client_socket.close()
if __name__ == "__main__":
main()
```
### Instructions
1. **Prepare the environment**:
- Ensure that Python is installed on both computers.
- Save the server code as `server.py` and the client code as `client.py`.
2. **Run the server**:
- On the computer that will be the server, open a terminal (or command prompt), navigate to the directory where `server.py` is located, and run:
```bash
python server.py
```
3. **Run the client**:
- On the computer that will be the client, open another terminal, navigate to the directory where `client.py` is located, and run:
```bash
python client.py
```
- When prompted, enter the IP address of the server computer (you can find this using `ipconfig` on Windows or `ifconfig` on Linux/Mac).
4. **Send a file**:
- When the server is running and waiting for connections, you can enter the filename (including the path if it's not in the same directory) of the file you wish to send.
5. **Check Received File**:
- The client will save the received file with a prefix `received_` added to the original filename in the same directory as the `client.py`.
### Note
- Make sure that any firewalls on either machine allow connections on the specified port (5001 in this example).
- Ensure both computers are connected to the same network, or make appropriate configurations if they're on different networks.