0

Struggling to find the correct phrase to look up similar questions so apologies if it's a duplicate.

I have the following code generated from Postman to send a simple HTTP request to 192.168.0.18. I am looking to create a file that will send the same request to every single IP from 192.168.0.10 through to 192.168.0.100 (or any other ranges) with the caveat being that there won't be a device on every IP

For reference, this is just a little gimmick in our training room. The laptops will respond to this request however there could be any number of laptops in the room at any one time, with any IP address in this range and this is a factor that I cannot change due to other limitations.

Thanks in advance

Request:

import requests

url = "http://192.168.0.18:4430/titan/set/2/Panel/Screen/LampIntensity"

payload="100"
headers = {
  'Content-Type': 'text/plain'
}

response = requests.request("POST", url, headers=headers, data=payload)


  • and what have been your attempts and at what part is this giving you any problem? – LotB Jun 19 '21 at 11:55
  • @LotB Short of copy and pasting the request, not much - am a very new learner hence asking for help. I think I need to use a "for i in range (10,100)" loop or similar, but struggling with mixing strings and integers into the URL – Adam Davies Jun 19 '21 at 12:02

2 Answers2

2
import requests

base_url = "http://192.168.0.{}:4430/titan/set/2/Panel/Screen/LampIntensity"

payload="100"
headers = {
  'Content-Type': 'text/plain'
}

for i in range(10, 100): # ip range
  try:
    response = requests.post(base_url.format(i), headers=headers, data=payload, timeout=1)
  except:
    print("No ip found: ", i)
OKEE
  • 450
  • 3
  • 15
1

expanding on SlLoWre's solution to use threads. so that it will not take long

import requests
import threading 
url = "http://192.168.0.{}:4430/titan/set/2/Panel/Screen/LampIntensity"

payload="100"
headers = {
  'Content-Type': 'text/plain'
}
def send_request(num):
    try:
        response = requests.request("POST", url.format(num), headers=headers, data=payload)
    except:
        pass
thread_list = []

for i in range(0,256):
    t = threading.Thread(target=send_request, args=(i,))
    thread_list.append(t)
    t.start()

for tr in thread_list:
    tr.join()

Furkan
  • 352
  • 3
  • 12