Open In App

Test the given page is found or not on the server Using Python

Last Updated : 21 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to write a Python script to test the given page is found or not on the server. We will see various methods to do the same.

Method 1: Using Urllib.

Urllib is a package that allows you to access the webpage with the program.

Installation:

pip install urllib

Approach:

  • Import module
  • Pass the URL in urllib.request() reading URLs
  • Now check with urllib.error containing the exceptions raised by urllib.request

Implementation:

Python3




# import module
from urllib.request import urlopen
from urllib.error import *
 
# try block to read URL
try:
    html = urlopen("https://www.geeksforgeeks.org/")
     
# except block to catch
# exception
# and identify error
except HTTPError as e:
    print("HTTP error", e)
     
except URLError as e:
    print("Opps ! Page not found!", e)
 
else:
    print('Yeah !  found ')


Output:

Yeah !  found

Method 2: Using requests

Request allows you to send HTTP/1.1 requests extremely easily. This module also does not come built-in with Python. To install this type the below command in the terminal.

Installation:

pip install requests

Approach:

  • Import module
  • Pass the Url into requests.head()
  • If response.status_code == 200 then server is up
  • if response.status_code == 404 then server in down

Implementation:

Python3




# import module
import requests
 
# create a function
# pass the url
def url_ok(url):
     
    # exception block
    try:
       
        # pass the url into
        # request.hear
        response = requests.head(url)
         
        # check the status code
        if response.status_code == 200:
            return True
        else:
            return False
    except requests.ConnectionError as e:
        return e
 
# driven code
url_ok(url)


Output:

True


Previous Article
Next Article

Similar Reads

An application to test the given page is found or not on the server using Python
Prerequisite: Python Urllib module In this article, we are going to write scripts to test whether the given page is found on the server or not with a GUI application. We need to Install Urllib Module to carry out this operation. Type this command in your terminal. pip install urllib Approach: Import urllib module.Read URL with urllib.request.urlope
2 min read
How to Fix - "pg_config executable not found" in Python
When working with databases, the psycopg2 library is commonly used to interact with PostgreSQL databases. However, while installing or working with psycopg2, you might encounter an error message that says "pg_config executable not found." This error occurs when the required PostgreSQL configuration tool (pg_config) cannot be located during the inst
2 min read
How to fix - "EnvironmentError: mysql_config not found" in Python
The "EnvironmentError: mysql_config not found" problem is frequently encountered by Python developers, especially when working with MySQL databases. It occurs when the Python interpreter is unable to find the MySQL configuration file (mysql_config) required for creating particular Python modules or packages that rely on MySQL. Error Syntax:Environm
3 min read
How to Fix The Module Not Found Error?
In this article, we are going to cover topics related to ' Module Not Found Error' and what the error means. the reason for the occurrence of this error and how can we handle this error. What is "ModuleNotFoundError"? A "ModuleNotFoundError" is a common error message in programming, particularly in languages like Python that depends upon modules an
5 min read
"How to Fix 'jupyter: command not found' Error After Installing with pip "
Encountering the "Jupyter command not found" error after installing Jupyter with pip can be frustrating. This common issue typically arises due to problems with the installation path or virtual environments. In this guide, we'll explore the root causes and provide step-by-step solutions to fix this error. Ensuring Jupyter Notebook is correctly inst
4 min read
Python | Handling no element found in index()
Sometimes, while working with Python lists, we are confronted with a problem in which we need to check whether an element is present in a list and also where exactly, which index it occurs. The convenient solution of it is to use index(). But problem with this can come that sometimes, the desired element might not be present in the list. Let's disc
5 min read
Test whether the elements of a given NumPy array is zero or not in Python
In numpy, we can check that whether none of the elements of given array is zero or not with the help of numpy.all() function. In this function pass an array as parameter. If any of one element of the passed array is zero then it returns False otherwise it returns True boolean value. Syntax: numpy.all( array ) Parameters: An array Return: Boolean va
2 min read
Stop Test Suite after N Test Failures in Pytest
The Python package which makes applications easy to write small, readable tests, and can scale to support complex functional testing is known as Pytest. The Pytest can have various checks, which keep running until it encounters the first failure or error. But if the user wants the test suite to run until it encounters N failures, he can use the max
2 min read
Get all text of the page using Selenium in Python
As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient API to access Selenium WebDrivers like Firefox, IE,
3 min read
Close specific Web page using Selenium in Python
Prerequisites: Selenium Basics, Selenium close() and quit() Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. The close() meth
2 min read