S
S
shkurinskiy2013-12-12 16:34:43
Python
shkurinskiy, 2013-12-12 16:34:43

How to check if a remote url is reachable using Python?

I would like to know how to use Python(3.3) to determine if a certain url address is available to me?
When calling a command urllib.request.urlopen('адрес')with a non-existent address, an error is generated. How to modify this code, or how it is possible to replace this call so that the program does not stop its work.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dbihbka, 2013-12-12
@shkurinskiy

try:
    urllib.request.urlopen('адрес')
except URLError:
    print 'Адреса не существует'

S
Slavka, 2013-12-12
@Slavka_online

Get to know the exceptions mentioned above, a powerful thing to know

S
shkurinskiy, 2013-12-12
@shkurinskiy

Thanks for the help
, only in my example it turns out that you need to do the following:

try:
        urllib.request.urlopen('адрес')
except:
        print("Oops!  That was no valid adress.  Try again...")

when adding a condition to except, the error text is still displayed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question