Ich schreibe einen Python-Code, um den Durchsatz zwischen Server und Client zu finden. Es basiert auf der Funktionalität von speedtest.net, wo ich eine Dummy-Datei zur Berechnung der Geschwindigkeit sende. Das Problem, mit dem ich konfrontiert bin, ist eine unzuverlässige Durchsatzleistung. Ich werde Ihre Vorschläge zu schätzen wissen. Hier ist der Code.Falscher Durchsatzwert in Python
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
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')
Ausgabe, wenn akki.txt Senden
Server Output
Server listening....
Got connection from ('10.143.47.165', 60902)
('Server received', "'Hello server!'")
Done sending
Client-Ausgabe Datei geöffnet
Raw timers: 1503350568.11 1503350568.11 1503350568.11
Total: 0.00499987602234
**Throughput: 204.805 K/sec.**
Successfully received the file
connection closed
Ausgang für ak.zip (die größere Datei)
Client-Ausgabe Datei
Total: 0.0499999523163
**Throughput: 20.48 K/sec.**
Successfully received the file
connection closed