Ping it? ~_^
> 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
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 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
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
> 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>
> --
> 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