I have created a python client which creates multiple sockets to upload/download files from a server. I noticed mulitple times that when downloading too much data at once, my router restarted and my internet was offline for about 3 minutes. I have read that this happens when the bandwidth limit is exceded. How can I set a bandwidth limit for all the sockets open at the time?
I could find this: https://pypi.org/project/aiothrottle/ But when using this module I can't use my sockets module anymore and need to use aiohttp which should mean to me to recreate my hole python client.
# simplified version of my code #
import socket
ip = "1.1.1.1"
port = 8080
def start():
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mysocket.connect((ip, port))
mysocket.send(str.encode("GET " + "Data" + "HTTP/1.1 \r\n"))
mysocket.sendto(str.encode("GET " + "Data" + "HTTP/1.1 \r\n"), (ip, port))
except socket.error:
print("e")
mysocket.close()
while i in range(4): # here I am creating multiple sockets to upload/download data from different hosts
t = Thread(target=start)
t.start()
How to change the code so that none of the threads, containing a socket, will exceed a bandwidth limit?
I think you just can set max speed for transfer and interval for check how much bytes thread download while this time.
interval = 1.0 #seconds
max_bandwidth = 50 * 1024 #kilobytes * 1024
Then use socket.recv for downloading chunks in loop and create temporary control variable for measure how much data thread downloaded in interval. If downloaded data amount is bigger than max_bandwidth * interval then sleep thread for interval value. Similar method use for sending.