Herausforderung: Überprüfen Sie das Webprotokoll der Website
Aufgabe
Swipe to start coding
Lass uns eine einfache Funktion erstellen, die eine grundlegende Anfrage an eine Website sendet und überprüft, ob HTTPS verwendet wird.
Deine Aufgabe ist:
- Sende eine Anfrage und überprüfe, ob der Antwortstatuscode
200
entspricht. Dies ist notwendig, um die Erreichbarkeit der gewählten URL zu prüfen. - Überprüfe, ob die Antwort-URL mit dem Teilstring
https
beginnt. Dies weist darauf hin, dass unsere Quelle das HTTPS-Webprotokoll verwendet. - Rufe die Funktion
check_https_protocol()
mit der entsprechenden URL als Argument auf, um sie zu überprüfen.
Hinweis
Obwohl unsere URL (
'http://codefinity.com'
) zunächst die Verwendung des HTTP-Protokolls anzeigt, könnte der Server sie gemäß seinen serverseitigen Konfigurationen auf HTTPS umleiten. Unser Ziel ist es, die Unterstützung von HTTPS durch den Server zu ermitteln, indem wir dessen Konfigurationen untersuchen. Bei Erfolg wird unsere Anfrage umgeleitet und die Antwort-URL beginnt mit'https'
.
Lösung
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)
War alles klar?
Danke für Ihr Feedback!
Abschnitt 2. Kapitel 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)
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen