Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 unreliable throughput output. I will appreciate your suggestions on the same. Here is the code.

server.py

Python
import socket

import os

port = 60000
s = socket.socket()
host = socket.gethostname()
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='akki.txt'
   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

Python
import socket
import time
import os

s = socket.socket()
host = socket.gethostname()
port = 60000

t1 = time.time()
s.connect((host, port))
s.send("Hello server!")

with open('received_file', 'wb') as f:
    print 'file opened'
    t2 = time.time()
    while True:

        data = s.recv(1024)

        if not data:
            break

        f.write(data)
        t3 = time.time()

print data
print 'Total:', t3 - t1
print 'Throughput:', round((1024.0 * 0.001) / (t3 - t1), 3),
print 'K/sec.'
f.close()
print('Successfully received the file')
s.close()
print('connection closed')


Output when sending akki.txt

Server Output

Server listening....
Got connection from ('10.143.47.165', 60902)
('Server received', "'Hello server!'")
Done sending


Client output file opened

Raw timers: 1503350568.11 1503350568.11 1503350568.11
Total: 0.00499987602234
**Throughput: 204.805 K/sec.**
Successfully received the file
connection closed
Output for ak.zip ( which is bigger file)


Client output file opened

Total: 0.0499999523163
**Throughput: 20.48 K/sec.**
Successfully received the file
connection closed


What I have tried:

The output is changing. Like, If I am using file with smaller size, the throughput is more, however if I am sending the file with bigger size, the throughput is less. I am looking for the solution to get the constant throughput speed between two peers.
Posted
Updated 21-Aug-17 15:38pm
v2

1 solution

change this line to
print 'Throughput:', round((1024.0 * 0.001) / (t3 - t1), 3),

print 'Throughput:', round((totalbytesreceived * 0.001) / (t3 - t1), 3),

where totalbytesreceived will be the size of the file received
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900