Jump to content
EduGeek EdSec 2026 is on Tuesday in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

We have an issue internally with network speed. In order to try and isolate the problem I made a client & server application in python

Both scripts can be turned into a --onefile exe files with pyinstaller

 

It's important to run both files from a device and not a network share.

 

The server side is a simple flask web server that sends a file instead of a webpage. I'm using the 1GB test file from thinkbroadband.

The test file needs to be called file.iso and in a subfolder called files.

 

 

from flask import Flask, send_file, request
import socket
import os
import sys

server_ip_address = socket.gethostbyname(socket.gethostname())
PORT = 50500

if getattr(sys, 'frozen', False):
   path = os.path.dirname(sys.executable)
else:
   path = os.path.dirname(__file__)

app = Flask(__name__)
app.debug = True

@app.route('/') 
def static_download():
   try:        
       print(f"Connection from {request.remote_addr}")        
       return send_file(path + "\\files\\file.iso", download_name='file.iso')
   except Exception as e:
       print(f"{str(e)}")    


if __name__ == '__main__':
   app.run(host=server_ip_address, port=PORT)

 

The client program downloads the test file every 5mins and calculates the average speed. It can cope if the server goes offline so you can move the server around. I have it writing a log to a network share.

 

import requests
import time
import os
import sys
import socket
from datetime import datetime

#setup log file 
user = os.environ['USERNAME']
output = f"\\\\server\\share$\\tools\\lantest\\logs\\{user.lower()}.txt"
if not os.path.exists(output):
   open(output,'w').close()

def write_log_file(data):
   f = open(output, 'a')
   f.write(f"[{gettime()}] {data}")
   f.close()       

#figure out path, different if executable
if getattr(sys, 'frozen', False):
   path = os.path.dirname(sys.executable)
   #print(f" * Executable Path = {path}")
else:
   path = os.path.dirname(__file__)


server_ip = input('Enter server ip address: ')
url = f"http://{server_ip}:50500/"
LOCALFILE = "file.iso"

#test that the server is available
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
location = (server_ip, 50500)
result_of_check = sock.connect_ex(location)

def gettime():
   now = datetime.now()
   return now.strftime("%Y-%m-%d %H:%M")    

if result_of_check == 0:
   print("Server Ready")

   
   while True: 

       #test server is available
       sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       location = (server_ip, 50500)
       result_of_check = sock.connect_ex(location)

       if result_of_check == 0:
           #print("Server Ready")
           start = time.perf_counter()

           with requests.get(url, stream=True) as r:
               r.raise_for_status()
               with open(LOCALFILE, 'wb') as f:
                   for chunk in r.iter_content(chunk_size=8192):
                       f.write(chunk)

           download_time = round(time.perf_counter() - start,2)
           #find out file size
           file_size = round((os.path.getsize(path + "\\file.iso")/1048576),2)
           os.remove(path + '\\file.iso')

           average_speed = round((file_size/download_time)*8,2)
           print(f"[{gettime()}] Filesize={file_size}MB Time={download_time}secs Avg Speed={average_speed}Mbit")
           write_log_file(f"Filesize={file_size}MB Time={download_time}secs Avg Speed={average_speed}Mbit\n")

       else:
           now = datetime.now()
           datestr = now.strftime("%Y-%m-%d %H:%M")
           print(f"[{gettime()}] Server {server_ip} offline")
           write_log_file(f"Filesize={file_size}MB Time={download_time}secs Avg Speed={average_speed}Mbit\n")

       time.sleep(300)
else:
   print("Server not found")

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...