Search code examples
pythonmultithreadingapiconnection-reset

How to intentionally reset a connection?


I am currently working on a project to download .mp4 files from links from a text document, and it works with this code

import urllib.request
import random
import threading

def request(line):
    urllib.request.urlretrieve(line, 'D:\\example_directory\\' +
                               str(random.randint(1000000, 9999999)) + "-69" +
                               str(random.randint(100, 999)) + "-" +
                               str(random.randint(1000000, 9999999)) + ".mp4")

with open('D:\\example_directory\\links.txt') as f:
    for line in f:
        print(line)
        threading.Thread(target=request, args=(line,)).start()

I want to use this code to download videos from a website which is built for streaming, and therefore caps the download speed after around 5 seconds to 120kb/s. I found a bypass for this limitation: resetting your connection. Resetting your connection can be manually achieved by plugging the ethernetcable in and out or turning WiFi on and off again.

I want to know if there is any way to reset the connection, without having to plugin and out the cable / turn on and off the WiFi. Like a package or function that can be imported, but also a glitch would help.


Solution

  • You can reset your connection, like you said, by reconnecting to the network. There is, as of my knowlegde, no module to intentionally reset a connection, but you can achieve a simular result using this method:

    • first, connect via Ethernet (cable)
    • then use the "winwifi" module, to reset your connection example:
    import winwifi
    def reset():
      winwifi.disconnect()
    

    because you are connected to ethernet, the connection is restored within a fraction of a second, and therefore, resets. I hope this helped you and have a great journey on your coding adventures ;)