2

I am making a minecraft name sniping bot. I have a list of tokens and minecrafts api lets you send three requests for each token, before blocking them. A request to it looks something like this:

requests.put(f'https://api.minecraftservices.com/minecraft/profile/name/{name}',
             headers={"Authorization": "Bearer " + token,
             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
             "Content-Type": "application/json"})

Now I could just use a simple for loop and send all the requests that way, but it seems to be really slow and I am pretty sure there are ways to speed it up. I've heard of multithreading, multiproccessing and asyncio, but I am not really sure which one of them would be the fastest one and how to use them in the most efficient way. I would really appreciate it if someone could help me to figure this out.

Karthik Nayak
  • 731
  • 3
  • 14
Bruh
  • 31
  • 3
  • Does this answer your question? [Python: How can i send multiple http requests at the same time? (like fork)](https://stackoverflow.com/questions/15752973/python-how-can-i-send-multiple-http-requests-at-the-same-time-like-fork) – doduo Mar 21 '21 at 12:55
  • Maybe helpful https://stackoverflow.com/q/63872924/13782669 – alex_noname Mar 21 '21 at 19:50

1 Answers1

0

I believe Python: How can i send multiple http requests at the same time? (like fork) answers your question! You're right to solve this problem with multithreading, try using concurrent.futures.

If you want to send 3 requests at a time, then make a thread pool with 3 workers as follows:

def send_mc_request(token):
   ...

from concurrent.futures import ThreadPoolExecutor

tokens = [token1, token2, ...]
num_requests_to_send = 3

with ThreadPoolExecutor(max_workers=num_requests_to_send) as executor:
  for token in tokens:
    for _ in range(num_requests_to_send):
      executor.submit(send_mc_request, token)
doduo
  • 41
  • 5
  • Ok so i tried using this code, I changed it up a bit, because I don't really have multiple urls, instead I have multiple tokens, so i just changed the for loop so it loops it for each token in a list and it did work, but it only sends one request per one token. Would it be possible to make it sends 3 requests per one token? Oh and btw you appearantely need to import concurrent.futures not features – Bruh Mar 21 '21 at 11:04
  • Good catch on my mistype. To send the same request 3 times, just put it in a basic for loop! – doduo Mar 21 '21 at 11:14
  • Ok so this seems to be working, but I am pretty sure there is a way to do it faster. It takes me 2.5 seconds to send all the requests and I've seen some other python based name snipers which sends 20 requests in 1 seconds – Bruh Mar 21 '21 at 11:26
  • That's... not a particularly helpful comment? Perhaps your network is slow? Perhaps there's another API you could be hitting? Just saying "I want this to be faster" is not a good way to ask for help on this website. The savings from threading increases as you send more requests. When testing sending 3 in parallel vs serially doesn't matter much. But when I sent 20, the threaded approach significantly improved. – doduo Mar 21 '21 at 12:51