0

So a module I need for my project requires SSL to install. I heard Ubuntu comes with Python with SSL support, so I checked if I got SSL on a fresh install of Ubuntu 16.04 LTS 64-bit using Oli's code snippet:

from urllib.request import urlopen
urllib.request.urlopen('https://askubuntu.com').read()

They said it should spit out some HTML if SSL is working properly, but all I got was this error:

Traceback (most recent call last):
  File "ssltest.py", line 2, in <module>
    urllib.request.urlopen('https://askubuntu.com').read()
NameError: name 'urllib' is not defined

Remember, this is a fresh install of Ubuntu 16.04, and I've seen numerous other answers that say it should come with SSL.

  • Do I need to install the urllid module?
  • Do I need to install some other module(s)?
  • Do I need to get some sort of SSL "certificate"?
  • Do I need to recompile Python and risk bricking my system? (Pray it not be this)
  • Do I need to reinstall Ubuntu?
  • Something else?

1 Answer 1

1

Something else?

The error is in your program only (and in the original post you refer to which I have fixed now). You have to use either the imported urlopen function:

from urllib.request import urlopen
urlopen('https://askubuntu.com').read()

or the imported urllib.request library:

import urllib.request
urllib.request.urlopen('https://askubuntu.com').read()

but not a mix of both, i.e. use a library where you've only imported a function like in your case.

4
  • Fixed it, thank you. But when I run the code, it just freezes for a few seconds and doesn't output anything...
    – dan9er
    Dec 10, 2016 at 23:34
  • @Dan9er: It might help if you would actually understand what the code is intended to do you've copied somewhere from the internet. This code gets the URL and that's it. It will fail if SSL is not installed, URL not reachable etc but no part of the code will actually do something with the retrieved data (i.e. output, write to a file etc). So "freeze" and just exit sounds like the intended behavior: it gets the URL (which takes some time) and then exits the program. Dec 11, 2016 at 6:17
  • Oh, it's one of those functions that returns a value? Hang on, let me try doing that...
    – dan9er
    Dec 11, 2016 at 16:00
  • Yep! It worked! All I did was wrap the urlopen() in a print() function and it spat a bunch of HTML, just like @oli said. Thank you so much!
    – dan9er
    Dec 11, 2016 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .