Home > Enterprise >  Slowing down the speed of Python Requests?
Slowing down the speed of Python Requests?

Time:04-22

I have written a bot for login using python. It works without any problem when I post on chrome using Selenium, burp suite and html form.

However, when I want to use it with the requests library, the 429 code returns.

The site has cloudflare. As a result of my research, when I post using selenium, there is no problem because it makes slow post processing.

However, when I use the requests library, the error 429 returns because it does not act as a browser and posts fast.

If I use the requests library, I need to slow down the speed, make it act like a browser, not post too fast.

Is there any solution to this?

**Note: There is no header return like Retry-After, I just need to slow down the request speed.

CodePudding user response:

Not Working;

import requests
import sys

def getToken(email,password):
  try:
    headers = {
    'accept':'text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'accept-encoding':'gzip, deflate, br',
    'accept-language':'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7,ht;q=0.6,nl;q=0.5',
    'cache-control':'max-age=0',
    'content-type':'text/plain'
    }
    raw = '{"Username":"' email '","Password":"' password '","GenerateToken":true}='
    login = requests.post('https://xxxxxx/login', data=raw, headers=headers)
    print(login.status_code)

except Exception as e:
    print(e)



getToken('[email protected]','xxxxx')

Working (I'm posting with Chrome.):

<html>
<body>
<form action="https://xxxxxx/login" method="POST" enctype="text/plain">
  <input type="hidden" name="&#123;&quot;Username&quot;&#58;&quot;[email protected]&quot;&#44;&quot;Password&quot;&#58;&quot;xxxxxx&quot;&#44;&quot;GenerateToken&quot;&#58;true&#125;" value="&#13;&#10;" />
  <input type="submit" value="Submit request" />
</form>

CodePudding user response:

It works when I post the html code with Selenium. I had to add the following settings;

options = Options()
options.add_argument("--headless")
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
s = Service('chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
stealth(driver,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
)
  • Related