Challenge: Check Website Web Protocol
Taak
Swipe to start coding
Let's create a simple function that will make a basic request to a website and check whether it uses HTTPS.
Your task is to:
- Make a request and check if the response status code equals
200
. It's needed to check the availability of the chosen URL. - Check if the response URL starts with subscring
https
. It will indicate that our source uses HTTPS web protocol. - Call the
check_https_protocol()
function with the corresponding URL as an argument to check it.
Note
While our URL (
'http://codefinity.com'
) initially indicates the use of the HTTP protocol, the server might redirect it to HTTPS as per its server-side configurations. We aim to ascertain the server's support for HTTPS by examining its configurations. If successful, our request will be redirected, and the response URL will commence with'https'
.
Oplossing
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import requests # Import the requests library for making HTTP requests
def check_https_protocol(url):
try:
# Send an HTTP GET request to the specified URL
response = requests.get(url)
# Check if the response status code is 200 (OK)
if response.status_code == 200:
# Check if the URL starts with 'https'
if response.url.startswith('https'):
return f'The website {url} uses HTTPS. It is secure.'
else:
return f'The website {url} does not use HTTPS. It is not secure.'
else:
# If the response status code is not 200, return an error message
return f'Error: Unable to connect to {url}.'
except requests.exceptions.RequestException as e:
# Handle any exceptions that may occur during the request (e.g., network issues)
return f'Error: {e}'
# Example usage:
url_to_check = 'http://codefinity.com'
result = check_https_protocol(url_to_check)
print(result)
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 2. Hoofdstuk 3
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import requests # Import the `requests` library for making HTTP requests
def check_https_protocol(url):
try:
# Send an HTTP GET request to the specified URL
response = requests.get(url)
# Check if the response status code is 200 (OK)
if response.status_code == ___:
# Check if the URL starts with 'https'
if response.url.startswith('___'):
return f'The website {url} uses HTTPS. It is secure.'
else:
return f'The website {url} does not use HTTPS. It is not secure.'
else:
# If the response status code is not 200, return an error message
return f'Error: Unable to connect to {url}.'
except requests.exceptions.RequestException as e:
# Handle any exceptions that may occur during the request (e.g., network issues)
return f'Error: {e}'
# Example usage:
url_to_check = 'http://codefinity.com'
result = ___(url_to_check)
print(result)
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.