1

I executed this code to connect most common api-

    import requests
    response = requests.get("http://api.open-notify.org/iss-now.json")
    print(response.status_code)

But it is showing this error -

    runfile('C:/Users/sanchit.joshi/use case of unassigned tickets/Api try 
    out.py', wdir='C:/Users/sanchit.joshi/use case of unassigned tickets')
    Traceback (most recent call last):

    File "<ipython-input-17-39bcdc5917ae>", line 1, in <module>
    runfile('C:/Users/sanchit.joshi/use case of unassigned tickets/Api try 
    out.py', wdir='C:/Users/sanchit.joshi/use case of unassigned tickets')

    File "C:\ProgramData\Anaconda3\lib\site- 
    packages\spyder_kernels\customize\spydercustomize.py", line 668, in 
    runfile
    execfile(filename, namespace)

    File "C:\ProgramData\Anaconda3\lib\site- 
    packages\spyder_kernels\customize\spydercustomize.py", line 108, in 
    execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

    File "C:/Users/sanchit.joshi/use case of unassigned tickets/Api try 
    out.py", line 8, in <module>
    response = requests.get("http://api.open-notify.org/iss-now.json")

    File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 
    72, in get
    return request('get', url, params=params, **kwargs)

    File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 
    58, in request
    return session.request(method=method, url=url, **kwargs)

    File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", 
    line 512, in request
    resp = self.send(prep, **send_kwargs)

    File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", 
    line 622, in send
    r = adapter.send(request, **kwargs)

    File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", 
    line 513, in send
    raise ConnectionError(e, request=request)

    ConnectionError: HTTPConnectionPool(host='api.open-notify.org', 
    port=80): Max retries exceeded with url: /iss-now.json (Caused by 
    NewConnectionError('<urllib3.connection.HTTPConnection object at 
    0x000001E8E5BCBE80>: Failed to establish a new connection: [Errno 11002] 
    getaddrinfo failed'))

I tried changing max retry value, but its not working. It is more frustrating bcz I this is the simplest code to connect to an api. Any help is appreciated.

2 Answers2

0

Using the JSON and urllib.request libraries

import json
import urllib.request

file = urllib.request.urlopen("http://api.open-notify.org/iss-now.json")
data = json.loads(file.read())
print(data)

Resulting in

{'message': 'success', 'timestamp': 1541059187, 'iss_position': {'longitude': '13.6813', 'latitude': '47.8641'}}
Faquarl
  • 335
  • 3
  • 11
0

There is nothing wrong with your code, that code should work.

You are having a proxy issue. If you are on windows you can add the url to your proxy exceptions by going to the settings menu in internet explorer, then internet options, then connections, lan settings advanced and add the url to your exceptions. It is typical in a corporate environment or in your school for the admins to put you behind a proxy.

Alternatively you can use this Q/A to set the proxy in your request

vencaslac
  • 2,727
  • 1
  • 18
  • 29