Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Testing for connection to a website

0 views
Skip to first unread message

Alexnb

unread,
Jul 15, 2008, 3:43:38 PM7/15/08
to pytho...@python.org

Okay, I already made this post, but it kinda got lost. So anyway I need to
figure out how to test if the user is able to connect to a specific website.
Last time I got pointed to the urllib2 page, but if I do urlopen() and and
am not connected, the program stops. So I don't know if that was what you
guys wanted me to do, but I don't think so, you guys are smarter than that.
So, how can I test for connection to a website.
--
View this message in context: http://www.nabble.com/Testing-for-connection-to-a-website-tp18473382p18473382.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jordan

unread,
Jul 15, 2008, 3:47:49 PM7/15/08
to
On Jul 15, 3:43 pm, Alexnb <alexnbr...@gmail.com> wrote:
> Okay, I already made this post, but it kinda got lost. So anyway I need to
> figure out how to test if the user is able to connect to a specific website.
> Last time I got pointed to the urllib2 page, but if I do urlopen() and and
> am not connected, the program stops. So I don't know if that was what you
> guys wanted me to do, but I don't think so, you guys are smarter than that.
> So, how can I test for connection to a website.
> --
> View this message in context:http://www.nabble.com/Testing-for-connection-to-a-website-tp18473382p...

> Sent from the Python - python-list mailing list archive at Nabble.com.

Ping it? ~_^

Grant Edwards

unread,
Jul 15, 2008, 3:49:19 PM7/15/08
to
On 2008-07-15, Alexnb <alexn...@gmail.com> wrote:

> Okay, I already made this post, but it kinda got lost.

No, it didn't get lost. Your question was answered and you
didn't like the answer.

> So anyway I need to figure out how to test if the user is able
> to connect to a specific website. Last time I got pointed to
> the urllib2 page, but if I do urlopen() and and am not
> connected, the program stops.

How long did you wait? Depending on what/how/where the network
is broken, it make take a minute or two for the connection
attempt to fail. You may not like it, but that's how TCP
works. You're concerned with TCP connections, so you're going
to have to live with it.

> So I don't know if that was what you guys wanted me to do,

Yes it was what we advised you to do.

> but I don't think so, you guys are smarter than that. So, how
> can I test for connection to a website.

Like we told you:

1) Open a connection using urllib or urllib2.
2) The attempt will either fail or succeed.
3) Proceed accordingly.

--
Grant Edwards grante Yow! HUGH BEAUMONT died
at in 1982!!
visi.com

Larry Bates

unread,
Jul 15, 2008, 4:00:11 PM7/15/08
to
Alexnb wrote:
> Okay, I already made this post, but it kinda got lost. So anyway I need to
> figure out how to test if the user is able to connect to a specific website.
> Last time I got pointed to the urllib2 page, but if I do urlopen() and and
> am not connected, the program stops. So I don't know if that was what you
> guys wanted me to do, but I don't think so, you guys are smarter than that.
> So, how can I test for connection to a website.

You can test DNS resolution by doing this:

import socket
#
# Check to make sure domain is legal and that I can resolve it to IP addr
#
try:
ipaddr=socket.gethostbyname(domain)
except socket.gaierror:
print "Internet connection or DNS error"

For the "connect to a specific website" portion you just have to wait until it
times out to determine failure.

-Larry

Alexnb

unread,
Jul 15, 2008, 6:48:15 PM7/15/08
to pytho...@python.org


Alexnb wrote:
>
> Okay, I already made this post, but it kinda got lost. So anyway I need to
> figure out how to test if the user is able to connect to a specific
> website. Last time I got pointed to the urllib2 page, but if I do
> urlopen() and and am not connected, the program stops. So I don't know if
> that was what you guys wanted me to do, but I don't think so, you guys are
> smarter than that. So, how can I test for connection to a website.
>

Just for anyone looking this up later here is code that works :)

from urllib2 import *
import urllib2
e = ''

req = urllib2.Request('http://www.dictionary.com')
try:
response = urlopen(req)

except HTTPError, e:
print e.code
except URLError, e:
print e.reason

if e == '':
print "good to go"
--
View this message in context: http://www.nabble.com/Testing-for-connection-to-a-website-tp18473382p18476597.html

Alexnb

unread,
Jul 15, 2008, 8:58:49 PM7/15/08
to pytho...@python.org


Timothy Grant wrote:

>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
> I'm glad to see you found the old post and implemented the code suggested
> there.
>
> --
> Stand Fast,
> tjg. [Timothy Grant]
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

well I can tel you it didn't work for me. In fact I didn't see the code you
posted on there, I ended up figuring it out on my own with a little help
from the missing manual for urllib2. Also, being rude is kinda stupid
because you don't have to help in the first place...
--
View this message in context: http://www.nabble.com/Testing-for-connection-to-a-website-tp18473382p18478103.html

Fredrik Lundh

unread,
Jul 16, 2008, 5:16:28 AM7/16/08
to pytho...@python.org
Alexnb wrote:

> e = ''

> try:
> ...


> except HTTPError, e:
> print e.code
> except URLError, e:
> print e.reason
>
> if e == '':
> print "good to go"

footnote: here's a better way to test if an exception was raised or not:

try:
...


except HTTPError, e:
print e.code
except URLError, e:
print e.reason

else:
print "good to go"

</F>

Alexnb

unread,
Jul 16, 2008, 2:29:24 PM7/16/08
to pytho...@python.org

> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

Thanks! can't believe I didn't think of that.

--
View this message in context: http://www.nabble.com/Testing-for-connection-to-a-website-tp18473382p18493785.html

0 new messages