S
S
Sl0n1k2020-01-14 19:05:23
Python
Sl0n1k, 2020-01-14 19:05:23

How to make more than 1 POST requests at the same time without waiting for a response?

Greetings.
Problem: You want to send more than one POST request to the server without waiting for a response (perhaps storing them later as they arrive) before each subsequent request.
With code like this, the program waits for a response after each request. The result is not a load on the API in the form of 10 simultaneous requests, but their sequential execution. How to send them together at the same time, and put the incoming answers in a queue and write them down?

from datetime import datetime
import random

import requests

import threading

import time

#Выбор стенда
hostUrl = {1: '1.2.3.4', 2: '1.2.3.5', 3: '1.2.3.6'}
print('Введите номер стенда для тестирования', '\n', '1 - Тестовый (' + hostUrl[1] + ')', '\n', '2 - Дев (' + hostUrl[2] + ')', '\n', '3 - Бой   (' + hostUrl[3] + ')', sep='')
urlSelect = str(hostUrl[int(input())])
hostAddr = urlSelect

#Генератор ip-адреса
ipRaw = []
for x in range(4):
  ipRaw.append(str(random.randint(1, 255)))
ipTrue = '.'.join(ipRaw)

#Время авторизации
loginTime = datetime.today().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]

#Функция отправки запроса
def userLogin(hostAddr, loginTime):
  UnencodedRequest = open("1.xml", "r")
  body = UnencodedRequest.read().encode("UTF-8")
  headers = {'Host': hostAddr,
         'Content-Type': 'text/xml;charset=UTF-8',
         'Content-Length': str(len(body)),
         'SOAPaction': 'http://tempuri.org/UserWcf/User',
         'Browser': 'AutoTest_' + loginTime,
         'Ip': ipTrue,
         'Application-Id': '1'}
  response = requests.post(url='http://' + hostAddr +'/UserWcf.svc?wsdl',
               headers=headers,
               verify=False,
               data=body)
  print(response.text + '\n', sep='')
  UnencodedRequest.close()

#Попытка отправки запроса десятью потоками
for t in range(10):
  threading.Thread(target=userLogin(hostAddr, loginTime)).start()
time.sleep(10)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-01-14
@Sl0n1k

The target parameter must receive a reference to the function, and in your case it receives the result of the function call.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question