Click here to Skip to main content
15,892,161 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 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

Python
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

Python
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)
Posted
Comments
Richard MacCutchan 23-Aug-17 4:19am    
You would need a separate connection between the client and server to run the upload test. But it will be very inefficient.
Member 14841130 28-Jun-20 16:09pm    
able to create zip folder at client with the size as well, but no files inside the zip folder at client. please sugest

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