[Tutor] URL test function.

bob gailer bgailer at gmail.com
Fri Oct 22 18:53:45 CEST 2010


On 10/22/2010 10:54 AM, Jeff Honey wrote:
> I am trying to create a function to plug into a restart script I'm creating. I can't seem to successfully loop through making an http connection, if it fails to retry, if it succeeds, to end and continue with its parent function.
>
> I'm  using the Fabric project's module and including my functions in fabfile.py.
>
> the most rudimentary I've got right now is this:
>
> <snip>
>
> def is_ready():
>   with settings(
>    warn_only=True
>   ):
>    try:
>     urllib2.urlopen('http://'+env.host_string+'\:8080')
>    except urllib2.URLError:
>     time.sleep(10)
>     urllib2.urlopen('http://'+env.host_string+'\:8080')
> </snip>
>
> I am trying to get the 'host_string' environment variable to plug in here but not sure if the parent function will pass along that information nor if I can call it like this.

I don't understand this part. Please clarify or show us the parent function.
> Additionally, I have never used try/except so I don't know if this will just try twice and quit. As it is now, it raises URLError and bombs out.
Whenever you want to repeat something you need either a loop or 
recursion. A loop is appropriate here.

   while True:
    try:
     urllib2.urlopen('http://'+env.host_string+'\:8080')
    except urllib2.URLError:
     time.sleep(10)
    else:
     break


-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list