服务器和客户端之间通过Python输出的吞吐量不正确 [英] Incorrect throughput output via Python between server and client
问题描述
我正在编写一个python代码来查找服务器和客户端之间的吞吐量。它基于speedtest.net功能,我发送一个虚拟文件来计算速度。我面临的问题是吞吐量输出不可靠。我将非常感谢你的建议。这是代码。
server.py
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
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
Client.py
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')
发送akki.txt时的输出
服务器输出
服务器监听....
从('获得连接10.143.47.165',60902)
('服务器收到',''你好,服务器!')
完成发送
客户输出文件已打开
原始计时器:1503350568.11 1503350568.11 1503350568.11
总计:0.00499987602234
**吞吐量:204.805 K /秒。**
成功收到文件
连接关闭>
ak.zip的输出(更大的文件)
打开客户端输出文件
总计:0.0499999523163
**吞吐量:20.48 K /秒。**
成功收到文件
连接关闭
我尝试过:
输出正在变化。就像,如果我使用较小尺寸的文件,吞吐量更高,但是如果我发送的文件更大,吞吐量更低。我正在寻找解决方案,以获得两个同行之间的恒定吞吐速度。
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.
推荐答案
将此行更改为
change this line to
print 'Throughput:', round((1024.0 * 0.001) / (t3 - t1), 3),
print 'Throughput:', round((totalbytesreceived * 0.001) / (t3 - t1), 3),
其中totalbytesreceived将是收到的文件的大小
where totalbytesreceived will be the size of the file received
这篇关于服务器和客户端之间通过Python输出的吞吐量不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!