سؤال

Is there a way beyond catching exceptions(HTTPError,URLError), to check if access to a url is permitted.

I have to constantly(every 30 secs) ping a url (using urllib2.open)(which might or might not be valid). And throwing and catching exceptions each time seems excessive. I couldn't find anything in the documentation.

هل كانت مفيدة؟

المحلول

There's nothing wrong with using try/except. That's the way to do such things in python.

it's easier to ask forgiveness than permission

نصائح أخرى

import requests
r = requests.get('http://httpbin.org/get')
>>> r.status_code == requests.codes.ok
True
>>> r.status_code
200

requests

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top