I have looked every where on the net on how to send a file in python, 100% is a fail, no one can help. Are there any programmers out there that can help me send a file from client to server or the other way?
I can send txt very easy
#!/usr/bin/python
"""
Socket Client
"""
import socket #networking library
indent = ""
server = input("server name (default is " + socket.gethostname() + "): ") or socket.gethostname()
print("connecting to server at: %s" % server)
while True:
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((server, 23000))
str = input("text to send: ")
clientSocket.send(str.encode("utf-8")) #send text as encoded bytes
print("received: %s" % clientSocket.recv(100).decode("utf-8"))
clientSocket.close()
#strRecv = clientSocket.recv(500).decode("utf-8") #receive up to 500 bytes and decode into text
#print(strRecv)
Very basic example:
import socket
with socket.socket() as s:
s.bind(('',8000))
s.listen(1)
with s.accept()[0] as c:
chunks = []
while True:
chunk = c.recv(4096)
if not chunk: break
chunks.append(chunk)
with open('out.txt','wb') as f:
f.write(b''.join(chunks))
import socket
with socket.socket() as s:
s.connect(('localhost',8000))
with open('myfile.txt','rb') as f:
s.sendall(f.read())