Skip to content

Instantly share code, notes, and snippets.

@FelixSchwarz
Created March 14, 2013 08:43
Show Gist options
  • Save FelixSchwarz/5159842 to your computer and use it in GitHub Desktop.
Save FelixSchwarz/5159842 to your computer and use it in GitHub Desktop.
def validate_python(self, value, state):
import logging
log = logging.getLogger(__name__)
log.info('URIValidator: %r' % value)
try:
splitted_url = urlsplit(value)
except:
self.raise_error_bad_url(value, state)
scheme = splitted_url[0] # '.scheme' in Python 2.5+
netloc = splitted_url[1] # '.netloc' in Python 2.5+
path = splitted_url[2] # '.path' in Python 2.5+
log.info('scheme: %r - netloc: %r - path: %r' % (scheme, netloc, path))
# Python 2.4 does not fill netloc when parsing urls with unknown
# schemes (e.g. 'rtmp://')
netloc_given = (len(netloc) > 0) or (path.startswith('//') and path != '//')
log.info('netloc_given? %r' % netloc_given)
if (scheme == '') or not netloc_given:
self.raise_error_bad_url(value, state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment