-->

Welcome to our Coding with python Page!!! hier you find various code with PHP, Python, AI, Cyber, etc ... Electricity, Energy, Nuclear Power

Showing posts with label networking. Show all posts
Showing posts with label networking. Show all posts

Tuesday, 3 July 2018

C# | Better way to code download speed calcuation

I'm writing a program that downloads stuff from the internet. While the program is working fine, I want to get one critical part reviewed. I use System.Diagnostics.StopWatch object to measure the time taken for the ns (NetworkStream) object to read the bytes in buffer. I then divide the number of elapsed seconds in stopwatch by number of bytes received to calculate the speed of my download in KBPS.

However, this method is giving me some extraordinarily large speeds (in excess of 8000KBPS), whereas I know that my internet bandwidth is not that fast. Is there anything wrong with this method? If not, then what is the correct way of getting the download speed? Here is my code:
while(true) //(bytesToRead>0)
{

    sw.Start();
    int n = ns.Read(buffer, 0, buffer.Length);
    sw.Stop();
    Speed = (float)n / sw.Elapsed.TotalSeconds;

    if (n==0)
        break;

    fs.Write(buffer,0,n);

    BytesRead+=n; //TODO: Persist the bytesRead object somewhere, so that it can be taken back for resumes
    bytesToRead -= n;
    OnProgress(this,new System.EventArgs());
    if (Status == DownloadStatusEnum.Paused) break;
}

Python | Internet Speed Calculator

This program calculates up and downspeed of a given connection for as long as it is running.

#upspeed/downspeed indicator
#written by Chubak -> aurlito@riseup.net

import requests
import time
import os

def get_seconds(datetime):
    time = datetime.split(' ')[3].split(':')

    time_hours = int(time[0])
    time_minutes = int(time[1])
    time_seconds = int(time[2])

    return time_hours*360 + time_minutes*60 + time_seconds




def downspeed():
    url = "http://speedtest.ftp.otenet.gr/files/test100k.db"

    current_seconds = get_seconds(time.asctime())

    file = requests.get(url)
    headers = file.headers

    file_size = int(headers['Content-Length'])/1000

    dl_seconds = get_seconds(time.asctime())

    time_difference = dl_seconds - current_seconds

    return round(file_size / time_difference)



def upspeed():
    current_seconds = get_seconds(time.asctime())

    dummy_file = os.path.join(os.getenv('APPDATA'), 'dummy.txt')
    post_url = 'http://httpbin.org/post'
    with open(dummy_file, 'wb') as dummy:
        for i in range (1500):
            dummy.write(str.encode('This is a dummy text. Its sole propose is being uploaded to a server. '))

        dummy.close()

    files = {'file' : open(dummy_file, 'rb')}

    request = requests.post(post_url, data=files)

    headers = request.headers

    file_size = int(headers['Content-Length'])/1000

    dl_seconds = get_seconds(time.asctime())

    time_difference = dl_seconds - current_seconds

    return round(file_size / time_difference)


if __name__ == '__main__':
    while True:
        up = None
        down = None

        if up == None and down == None:
            up = upspeed()
            down = downspeed()

            print('At {0} your Downspeed is: {1}, and your Upspeed is: {2}'.format(time.asctime(), down, up))

            time.sleep(5)

            up = None
            down = None
I'd appreciate it if your dropped any hints about turning this into a desktop widget.

Rank

seo