NameError: name 'urllib' is not defined TypeError:'module' object not callable

Viewed 951

Trying to execute this code. But i get the above error.

from urllib.request import FancyURLopener
from urllib.request import urlopen
exam = urllib.request.urlopen("192.168.2.2")
    print(exam.read)
    exam.close()

I expect to open the provided IP address.

1 Answers
from urllib.request import FancyURLopener
from urllib.request import urlopen
exam = urlopen("http://192.168.2.2")
print(exam.read)
exam.close()

Try using urlopen("url") rather than the full urllib.request.urlopen(url).

You need to add http or https on your URL to make this work. Also, check out python requests, a good alternative to urllib.

Related