Python Forum

Full Version: Connectivity issue with urllib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I think I'm having some issues with urllib but I'm not sure what exactly is the problem.

While trying a basic code as :

import urllib
import urllib.request

def connected(host='http://www.google.com'):
    try:
        urllib.request.urlopen(host)
        return True
    except:
        return False

# test
print( 'connected' if connected() else 'no internet!' )
While running this code through PyScripter or PyCharm I'm getting 'no internet!' message
while running under git Bash I'm getting 'connected!'

Any idea what could be the difference?

This is just a basic test as I'm trying to connect to P4 through p4python and to Rally with pyral.
I'm connected through corporate proxy.

Thanks!
Ok, some more details.
Now it doesn't work at all.
I'm using PyScripter 3.4.2 (64) and Python 3.6.7 (64)
But in anyway, in git bash it does not work at all (getting 'Not Connected' msg)

Can anyone help with commands on how to find the problem?
Perhaps firewall? proxy?
Run the interpreter, import urllib and try urllib.request.get('https://google.com').getcode() to see if you get 200.

If you just want to check for the internet connection there are services which return your public IP.
Thank you for your reply.

This is the error msg that I'm getting -


Traceback (most recent call last):
File "Connect2INTERNT.py", line 43, in <module>
main()
File "Connect2INTERNT.py", line 40, in main
urllib.request.get('https://google.com').getcode()
AttributeError: module 'urllib.request' has no attribute 'get'
Sorry! My bad. I am using requests for too long. Big Grin
There is not get method but urlopen instead.

urllib.request.urlopen('https://google.com').getcode()
oh ok :)

That didn't work at all... here is the output (copied only the err msg):

socket.gaierror: [Errno 11004] getaddrinfo failed

urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
Perhaps there is some DNS issue.
It works for me.

In [1]: import urllib

In [2]: urllib.request.urlopen('https://google.com').getcode()
Out[2]: 200
What checks can I do to see where is the problem?
If it is DNS, Security, Firewall...