Sending any file from the client to the server over the network using tcp:
Server code:
#filename: file_recv.py
#usage: python3 file_recv.py 0.0.0.0 5555 cat2.jpg
#file transfer demo. Recieve file (server)
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = sys.argv[1]
port= int(sys.argv[2])
s.bind( (host, port) )
s.listen()
conn, addr = s.accept()
data=True
with open(sys.argv[3], 'wb') as f:
while data:
data = conn.recv(1024)
f.write(data)
conn.close()
Client code:
#filename: file_send.py
#usage: python3 file_send.py 192.168.x.x 5555 cat.jpg
#file transfer demo. Sender (client)
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = sys.argv[1]
port = int(sys.argv[2])
s.connect( (host, port) )
data=True
with open(sys.argv[3], 'rb') as f:
while data:
data=f.read(1024)
s.sendall(data)
s.close()