Guide for testing URL connectivity using Python

My scenario is similar to the one described in this Stack Overflow post.

However, I want to log an error message if the URL cannot be reached. Here's my attempt:

# Defining the URL and checking the connection by printing the status

url = 'https://www.google.lk'

try:
    page = requests.get(url)
    print(page.status_code)
except requests.exceptions.HTTPError as err:
    print("Error")

The problem is that instead of just printing "Error," it outputs a verbose error message like this:

Traceback (most recent call last):
  File "testrun.py", line 22, in <module>
    page = requests.get(url)
  (...)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='learn.microsoft.com', port=443): Max retries exceeded with url: /en-us/microsoft-365/enterprise/urls-and-ip-address-ranges?view=o365-worldwide (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7ff91a543198>: Failed to establish a new connection: [Errno 110] Connection timed out',))

Could someone please advise me on how to modify my code so that only the word "Error" is printed if there is an issue, allowing for further customization based on requirements?

Answer №1

For those looking to verify the accessibility of a URL for POST requests without actually sending any data to the API, I suggest the following method:

import requests

url = 'https://www.example.com'

try:
    response = requests.options(url)
    if response.ok:   # you can also use response.status_code == 200
        print("Success - API is accessible.")
    else:
        print(f"Failure - API is accessible but something is not right. Response code : {response.status_code}")
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e:
    print(f"Failure - Unable to establish connection: {e}.")
except Exception as e:
    print(f"Failure - Unknown error occurred: {e}.")

Attempting to use a GET request to check the existence of a POST Endpoint would result in an HTTP 405 – Method Not Allowed error, which can be problematic. However, using requests.options() will return HTTP 200.

Answer №2

You are failing to capture the specific exception.

import requests


url = 'https://www.googlggggggge.lk'

try:
    page = requests.get(url)
    print(page.status_code)
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError):
    print("There was an issue")

Alternatively, you could use except Exception, although it is cautioned against as it encompasses all errors and may not be ideal in most scenarios

Answer №3

In order to handle exceptions when using the requests module in Python, you have two options. You can either use a general exception block with except, or specify specific exceptions to catch such as

except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError)
.

For a complete list of exceptions and how to properly handle them with the Python requests module, refer to: Correct way to try/except using Python requests module?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Selenium bot designed for leaving comments on YouTube videos is having trouble locating the comment box on the

Seeking assistance urgently. Provided below is the complete code snippet for logging into YouTube using Selenium's .find_element methods. I am struggling to navigate to specific videos, scroll down, and select a YouTube comment box. Despite trying v ...

Perform a task X number of times every X hour using Python and Django

I am looking to create a program that can perform a task a specified number of times within a given time frame. For example, I want the task to be done 10 times every hour, or 10 times every day, or 3 times every day. I want this to be customizable by prov ...

Python 3.9.5: A possible bug where a single dictionary assignment is replacing multiple keys

Currently, I am parsing through a .csv file named courses. In this file, each row represents a different course containing an id, a name, and a teacher. My goal is to store this information in a dictionary. For instance: list_courses = { 1: {'id& ...

Tips for displaying only the decimal component in heatmap annotation floats

I have decimal floats and I'm only concerned with the numbers after the decimal point, as the integer part is irrelevant for my problem. When displaying these numbers on seaborn plots, I need to use string formatting. Currently, I am using '0.2f ...

In Python, use a loop to assign different values to various index positions within a list

In my current project, I am working with a variety of data types including Lists, float values, and a NumPy array. dt = list(range(1, 12)) c = 18 limit = 2.75 Energy = np.zeros(len(dt)) My goal is to assign the value c = 18 in the NumPy array Energy. ...

Tips for locating and interacting with a specific element using Selenium

I am delving into the realm of Python and honing my skills by creating practical applications for daily use. I recently embarked on automating a routine task, but hit a roadblock when attempting to utilize selenium to click on a specific element that dynam ...

Bringing in text using pandas in Python

I need help regarding importing a table into a pandas dataframe. One of the strings in the table contains the special character 'NF-κB' with the 'kappa' symbol. However, when I use pd.read_table to import the table from 'table_pro ...

What is the best way to extract videos from a YouTube search?

I am looking to search for a specific keyword and then extract all the URLs of videos. Although I understand that the code I'm going to share won't achieve this goal, I still want to demonstrate my progress so far. chrome_path = r"C:\Users ...

How to halt a pygtk operation

I am currently working on a project that involves creating an application to control the playback of a midi file using pygtk. Here's what I have developed so far: import pygtk pygtk.require('2.0') import gtk import subprocess class Music ...

Generate random numbers using a probability distribution skewed to the left

I am interested in selecting a number randomly between 1 and 100 in such a way that the likelihood of getting numbers from 60 to 100 is higher than those from 1 to 59. My goal is to create a left-skewed distribution for numbers 1-100, meaning it should ha ...

What is the best way to retrieve a subprocess in a Flask application?

I have a question regarding my Python Flask script. I am fairly new to Python and Flask, so please bear with me. The second decorator in my code is supposed to return the subprocess to the /results page. While the ping test does print in the terminal, I ...

Is it true that sklearnex (sklearn-intel-extension) provides support for linear regression models?

Currently, I am exploring the use of sklearnex/scikit-learn-intelex for GPU acceleration. The code snippet below is what I have implemented based on the instructions provided in 'Patching several algorithms': try: from sklearnex import patch_ ...

Encountering Error 401 while attempting to retrieve course materials through Google Classroom API using Python

As a newcomer to Google APIs, I decided to experiment with the Classroom API. While using the provided sample code, I successfully retrieved a list of courses. However, when attempting to access coursework, topics, or announcements for a specific class, I ...

How to set up a static webdriver fixture and a generator in Pytest?

I am currently developing an automation test to detect any potential dead links in a WordPress plugin. In order to achieve this, I have implemented two helpful functions. The first function initializes a Selenium webdriver: @pytest.fixture(scope='ses ...

Issue: Unable to index a 'dict_values' object.Solution: 'dict_values' object does not support

While experimenting with the Kaggle Bag of Words module, I encountered an issue that stems from the following code snippet: kmeans_clustering = KMeans( n_clusters = num_clusters ) idx = kmeans_clustering.fit_predict( word_vectors ) word_centroid_map = dic ...

How can you run a multi-line query from Python in Postgres using psql on a Windows system?

My goal is to run multiline queries using psql, so I started working on a library function for that purpose. However, I encountered a "Permission denied" error while trying to execute the code. import os import tempfile sql = 'select 1;' with t ...

Transforming categorical data within a Pandas dataframe

My DataFrame contains a variety of data types across many columns: col1 int64 col2 int64 col3 category col4 category col5 category Here's an example of one of the columns: Name: col3, dtype: category Categories (8, ...

Tips for locating an element B that is positioned beneath another element A

Imagine you have the following elements on a webpage: Element A #A is found here ... #Some code Element B #B is located here Even though A and B do not have a parent-child relationship, they share the same locator. There are no element ...

Retrieve the value from a specific div among several divs with identical names

I am searching for the TTM EPS value and I need to extract it from this link Below is the code that I have written so far: import os from webdriver_manager.chrome import ChromeDriverManager import time from selenium import webdriver from selenium.webdrive ...

Steps for combining 2 data frames

Here is a table (table1) I have: A B C D 0 1 2 k l 1 3 4 e r When I use df.dtypes, it shows me the data types as follows: A int64 B int64 C object D object My goal now is to create another table (table2) containing only the columns with object data t ...