title: Python 3 - Router Access
body: <p>I have been using the following script to grab the PIN from our router. It is changed often so I decided to use a script so it would be easier than having to access the router from the browser.</p>
  
  <p>The script is as follows:</p>
  
  <pre><code>from requests.auth import HTTPBasicAuth
  import requests
  import re
  
  while True:
      try:
          response = requests.get('http://192.168.2.1/settings.html', 
          auth=HTTPBasicAuth('username', 'password'))
          html = response.content
          m = re.findall(b'var routerpin\s+=\s+(.*)', html)
          break
      except:
          m = None
  
  print(m)
  </code></pre>
  
  <p>The trouble I am having is the first time the script is run the variable 'm' returns an empty list. It does not give an exception. I thought by using a try - except loop and using the None or empty set as the exception would allow it to work.</p>
  
  <p>When the script runs once it returns m = []
  after this the script returns the correct data. I know this is down to the first run not authenticating with the router but not sure how I can handle it to run twice and grab the data.</p>
  
  <p>Probably a really simple answer but any help much appreciated.</p>