Python Forum
How to make each thread send multiple requests in python3?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make each thread send multiple requests in python3?
#1
I have written this script below and wanted to see how I can edit the script so that each thread sends multiple requests? Any suggestions how I can achieve this?

def get_data(host, url, req_id):
    conn = http.client.HTTPSConnection(host,timeout=10, context=ssl._create_unverified_context())
    headers = {
        "Accept":"application/json",
    }
    start = time.process_time()
    print("Sending Req: {}".format(req_id))
    conn.request("GET", url, headers=headers)
    response = conn.getresponse()
    request_time = time.process_time() - start

    print("Req: {} Status: {}{} ResponseTime: {}".format(req_id, response.status, response.reason, request_time))

if __name__ == "__main__":
    url = "/maps"
    host = "google.com"

    nr_threads = 10
    if len(sys.argv) == 2:
        nr_threads = int(sys.argv[1])
    try:
        threads = []
        for i in range(nr_threads):
            t = Thread(target=get_data, args=(host, url,i))
            t.start()
            threads.append(t)
        for t in threads:
            t.join()
    except Exception as e:
        print("Exception: ", e)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 List in requests ogautier 1 1,194 Mar-04-2022, 12:09 PM
Last Post: DeaD_EyE
  Python3 requests.models.Response ogautier 4 5,395 Feb-17-2022, 04:46 PM
Last Post: ogautier
  How to make scraper send email notification just once themech25 0 1,386 Nov-08-2021, 01:51 PM
Last Post: themech25
  convert string into multiple columns in python3 VAN 2 2,761 Sep-26-2020, 11:14 PM
Last Post: scidam
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,576 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,905 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Using a file to send multiple contacts Text via GSM sim 01001B 0 2,022 Sep-14-2019, 05:41 PM
Last Post: 01001B
  how this to make a asyncio thread safe? linziyan 0 2,353 Jun-07-2018, 10:33 AM
Last Post: linziyan
  Is this a normal/benign make test error when building python3.6 sofuego 2 3,513 Feb-12-2018, 12:32 AM
Last Post: sofuego
  thread in python3 tony1812 1 2,958 Sep-15-2017, 12:13 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020