What do you think about this?

def is_http_url(s):
    Returns true if s is valid http url, else false 
    - `s`:
    if re.match('https?://(?:www)?(?:[\w-]{2,255}(?:\.\w{2,6}){1,2})(?:/[\w&%?#-]{1,300})?',s):
        return True
        return False

import utils
class TestHttpUrlValidating(unittest.TestCase):
    def test_validating(self):

Is this enough? I'm going to insert URLs into database. Are there other ways to validate it?

  • \$\begingroup\$ I'd use urlparse in the standard library to check it. \$\endgroup\$ Commented Dec 22, 2012 at 5:23
  • 1
    \$\begingroup\$ You can use the rfc3987 package. rfc3987.match('http://http://codereview.stackexchange.com/', rule='URI') \$\endgroup\$ Commented Jan 8, 2014 at 19:58

1 Answer 1


I would check out Django's validator - I'm willing to bet it's going to be decent, and it's going to be very well tested.

regex = re.compile(
    r'^(?:http|ftp)s?://' # http:// or https://
    r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
    r'localhost|' # localhost...
    r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
    r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
    r'(?::\d+)?' # optional port
    r'(?:/?|[/?]\S+)$', re.IGNORECASE)

This covers a few edge cases like IP addresses and ports. Obviously some stuff (like FTP links) you might not want to accept, but it'd be a good place to start.

  • \$\begingroup\$ This code example seems mostly copied from that SO answer. I think it should have proper attribution. stackoverflow.com/a/7160778/172132 (Unlike the original answer this checks for IPv6 URLs, though.) \$\endgroup\$
    – cimnine
    Commented Feb 4, 2018 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.