0

I've been working through a LinkedIn Learning course trying to learn some Python, but I've run into a problem that's stopped my progress. I'm trying to work with JSONs and pull data from a website, but I keep getting an error saying that "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host failed to respond".

I'm using VSCode and have tried on both my work's network (which is heavily restricted, though not to this webpage for browsing) and on my home network. Is there some sort of network permission that would be stopping access? I experienced the same issue when trying to complete an API training course that used the OpenNotify API.

This is the code I'm trying to use.

import urllib.request

def main():
  webUrl = urllib.request.urlopen("https://www.google.com")
  print("result code: " + str(webUrl.getcode()))

  
if __name__ == "__main__":
  main()

1 Answers1

1

As ping works, but telnet to port 80 does not, the HTTP port 80 is closed on your machine. I assume that your browser's HTTP connection goes through a proxy (as browsing works, how else would you read stackoverflow?). You need to add some code to your python program, that handles the proxy.

You can take a look at here for more details info.

But why don't you try requests library, it is pretty much straightforward and easy to use also.
Heres some example:

>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
'{"type":"User"...'
>>> r.json()
{'private_gists': 419, 'total_private_repos': 77, ...}

You can just start using it by doing pip install requests and here's the documentation.

imxitiz
  • 3,920
  • 3
  • 9
  • 33