I have a question about some Python socket programming. I'm pretty new to sockets and probably just have a poor understanding of how they work. I need to transfer a binary file between two machines while running a python application and figured doing some socket programming would be better/faster than doing a system command scp or netcat. For some reason though, if I transfer a 1GB image between my machines it takes ~14 seconds. If I do it via SCP it only takes about 9. This seems odd since SCP is notoriously slow. I was hoping my transfer speeds would be comparable to netcat but I'm having trouble achieving this.
Server:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 50000))
s.listen(1)
img = open('test.img', 'rb')
client, addr = s.accept()
l = img.read(4096)
while(l):
client.send(l)
l = img.read(4096)
img.close()
s.close()
Client:
host = ''
port = 50000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
img = open('./newimg.img', 'wb')
l = s.recv(4096)
while(l):
img.write(l)
l = s.recv(4096)
img.close()
s.close()
This is obviously extremely basic and lacks a lot of features and exception handling but it at least gets a file to transfer (slowly though). I've obviously removed a few details from my code like the 'host' variable.
How can I improve this?
Thanks!