I am writing a python code to find throughput between server and client. It is based on speedtest.net functionality where I am sending a dummy file to calculate the speed. The problem I am facing is unidirectional throughput. How to work on transferring files from client to server and measure the upload speed as well as the same time? I will appreciate your suggestions on the same. Here is the code.
Server.py
import socket
import os
port = 60000
s = socket.socket()
host = socket.gethostname()
print host
s.bind((host, port))
s.listen(5)
print 'Server listening....'
while True:
conn, addr = s.accept()
print 'Got connection from', addr
data = conn.recv(1024)
print('Server received', repr(data))
filename = 'ak.zip'
b = os.path.getsize(filename)
f = open(filename, 'rb')
l = f.read(b)
while (l):
conn.send(l)
l = f.read(b)
f.close()
print('Done sending')
conn.send('Thank you for connecting')
conn.close()
Client.py
import socket
import time
import os
s = socket.socket()
host = socket.gethostname()
port = 60000
print host
t1 = time.time()
s.connect((host, port))
s.send("Hello server!")
with open('received_file1', 'wb') as f:
print 'file opened'
t2 = time.time()
while True:
data = s.recv(1024)
f.write(data)
t3 = time.time()
if not data:
break
d = os.path.getsize('received_file1')
print data
print d
print 'Total:', t3 - t1
print 'Throughput:', round((d * 0.001) / (t3 - t1), 3),
print 'K/sec.'
f.close()
print('Successfully received the file')
s.close()
print('connection closed')
Output on client side file opened
Total: 0.0469999313354
Throughput: 136213.986 K/sec.
Successfully received the file
connection closed
What I have tried:
Have worked to find throughput in one direction, but unable to do so for the other direction (at the same time)