10 examples of 'python check if url is valid' in Python

Every line of 'python check if url is valid' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
18def _is_valid_url(url):
19 """ Check if path is URL or not
20 Parameters
21 ----------
22 url : str
23 path to check
24 Returns
25 -------
26 bool
27 """
28 try:
29 result = urlparse(url)
30 return (
31 result.scheme
32 and result.netloc
33 and result.path
34 and (requests.get(url).status_code == 200)
35 )
36 except Exception:
37 return False
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1def is_valid_url(url):
2 return True
78def check_url(url):
79 result = urllib.parse.urlparse(url)
80 if result.scheme != 'https' or not re.fullmatch(netloc_re, result.netloc):
81 raise RuntimeError('Reached invalid URL: %r' % url)
226def _is_valid_http_url(url):
227 """
228 Return `True` if `url` is a valid HTTP or HTTPS URL.
229
230 Parsing is currently very lenient as the URL only has to be accepted by
231 `urlparse()`.
232 """
233 try:
234 parsed_url = parse.urlparse(url)
235 return parsed_url.scheme == "http" or parsed_url.scheme == "https"
236 except Exception:
237 return False
139def validate_url(url):
140 """validate a url for zeromq"""
141 if not isinstance(url, string_types):
142 raise TypeError("url must be a string, not %r"%type(url))
143 url = url.lower()
144
145 proto_addr = url.split('://')
146 assert len(proto_addr) == 2, 'Invalid url: %r'%url
147 proto, addr = proto_addr
148 assert proto in ['tcp','pgm','epgm','ipc','inproc'], "Invalid protocol: %r"%proto
149
150 # domain pattern adapted from http://www.regexlib.com/REDetails.aspx?regexp_id=391
151 # author: Remi Sabourin
152 pat = re.compile(r'^([\w\d]([\w\d\-]{0,61}[\w\d])?\.)*[\w\d]([\w\d\-]{0,61}[\w\d])?$')
153
154 if proto == 'tcp':
155 lis = addr.split(':')
156 assert len(lis) == 2, 'Invalid url: %r'%url
157 addr,s_port = lis
158 try:
159 port = int(s_port)
160 except ValueError:
161 raise AssertionError("Invalid port %r in url: %r"%(port, url))
162
163 assert addr == '*' or pat.match(addr) is not None, 'Invalid url: %r'%url
164
165 else:
166 # only validate tcp urls currently
167 pass
168
169 return True
16def is_url(url):
17 """
18 Returns an integer representing validity of url syntax
19
20 Args:
21 url (str): url to be verified
22 Returns
23 (int): integer representing if url is a valid format
24 """
25 pattern = r"^https?:\/\/(www\.)?([a-z,A-Z,0-9]*)\.([a-z, A-Z]+)(.*)"
26 regex = re.compile(pattern)
27 if regex.match(url):
28 return 1
29 return 0
14def is_url(url):
15 """Check if the given url is valid"""
16 return check_url(url, 'url')
111@property
112def valid_url(self):
113 if self.initialized:
114 url = self._driver.current_url
115 if url and url.startswith('http'):
116 return True
117 return False
50def _is_url(url):
51 parsed = urlparse(url)
52 return parsed.scheme != '' and parsed.netloc != ''
20def check(url):
21 """Ex:
22
23 >>> url = 'http://picasaweb.google.fr/ceronjeanpierre/'
24 >>> url += 'PhotosTriEsDuMariage#'
25 >>> check(url)
26 True
27 >>> check('http://picasaweb.google.com')
28 False
29 """
30 starts = url.startswith("http://picasaweb.google")
31 starts_https = url.startswith("https://picasaweb.google")
32 url_splited = url.split('/')
33 return (starts or starts_https) and len(url_splited) > 4

Related snippets