title: Python incrementing IP range in for loop
body: <p>I am trying to increment the start_ip and end_ip each iteration by 1. Although whenever the function results in the else statement 'already active' the counter keeps going. Is there a way to make number1 pause in the for loop when it hits ip is active? I tried decrementing it by one and adding a break but that does not seem to work.</p>
  <pre><code>#!/usr/bin/env python3
          
  import requests
  import time
  import re
          
  headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0','Accept-Encoding': 'gzip, deflate','Upgrade-Insecure-Requests': '1','Accept-Language': 'en-US,en;q=0.5'}
          
  def log(ip):  
                log = open(&quot;ip_active.txt&quot;,&quot;a+&quot;)
                log.write(ip)
                log.close
      
       
  def start_ip():
         x = 0
         while x == 0:
           for number1 in range(1,254):
            
             start_ip = '192.168.' + str(number1) + '.1'
             end_ip = '192.168.' + str(number1) +'.254'
             
             with open('ip_list.txt', 'r') as iplist:
              for ip in iplist:      
                 with open('ip_active.txt', 'r') as ipactive:
                  try:
                     if(ip not in ipactive):   
                       log(ip)
                       print('Running task: ' + ip +f'task.exe%20{start_ip}%20{end_ip}')
                       target = ip.strip()+f'task.exe%20{start_ip}%20{end_ip}'
                       r = requests.get(target, allow_redirects=True, verify=False, headers=headers, timeout=1)
                       
                     else:
                       #Here number1 keeps incrementing as well but should not.
                       print(f'ip {ip} already active')  
                       time.sleep(2)
                      
                                    
                  except:
                     print('Timeout')
                     continue    
                
  def main():
         start_ip()
      
        
  if __name__ == &quot;__main__&quot;:
          main()
  </code></pre>
  <p>Output:</p>
  <p><code>ip http ://172.16.86.153 already active ip http ://172.16.86.152 already active Running task: http://172.16.86.153/?747fb083889d4d3591b8e185032f958c= task.exe%20192.168.**1**.1%20192.168.**1**.254 Running task: http://172.16.86.152/?93440f9cf9f743f2b5e4269ec73d4b78= task.exe%20192.168.**2**.1%20192.168.**2**.254 ip http ://172.16.86.153 already active ip http ://172.16.86.152 already active</code></p>
  <p>It should continue with:</p>
  <p><code>Running task: http ://172.16.86.152/?93440f9cf9f743f2b5e4269ec73d4b78= task.exe%20192.168.**3**.1%20192.168.**3**.254 Running task: http ://172.16.86.153/?93440f9cf9f743f2b5e4269ec73d4b78= task.exe%20192.168.**4**.1%20192.168.**4**.254</code></p>
  <p>but now it results in for example :</p>
  <p><code>Running task: http ://172.16.86.152/?93440f9cf9f743f2b5e4269ec73d4b78= task.exe%20192.168.**120**.1%20192.168.**120**.254 Running task: http ://172.16.86.153/?93440f9cf9f743f2b5e4269ec73d4b78= task.exe%20192.168.**121**.1%20192.168.**121**.254</code></p>
  <p>As can be seen when an IP is available it is running the task for example with IP 192.168.12.1 192.168.12.254, for the next IP: 192.168.13.1 192.168.13.254, next ip 192.168.14.1 192.168.14.254 and so on. However when an IP is already active it keeps counting, so it skips those for Running tasks.</p>