Querying Line Status not working in Python

I have tried many different ways to query line status in Python but getting “<urlopen error [Errno 11001] getaddrinfo failed>” each time. Any help is appreciated. Using below code snippet.

########### Python 3.2 #############

import urllib.request, json

try:
    url = "https://api.tfl.gov.uk/Line/Northern/Status?app_key=abc1234"

    hdr ={
    # Request headers
    'Cache-Control': 'no-cache',
    }

    req = urllib.request.Request(url, headers=hdr)

    req.get_method = lambda: 'GET'
    response = urllib.request.urlopen(req)
    print(response.getcode())
    print(response.read())
except Exception as e:
    print(e)

####################################

Welcome @AnilK

Put a slash before the “?” as you’re probably got two overlapping parameter providing specs (uri and the old http-get one)

https://api.tfl.gov.uk/Line/Northern/Status/?app_key=17....

Thanks @briantist. I still see Errno 11001 getaddrinfo failed.

Any other ideas?

Hello and welcome to the forum!

Firstly, just to note that I’ve taken the liberty of putting the code in your original post in a code block for readability.

I’ve tried running your script locally and it worked fine. Perhaps there is some kind of networking issue on your end? What happens if you try opening that web address in a browser?

Replace api.tfl.gov.uk with 104.16.98.104 - the error suggests your DNS isn’t configured.

If you can set it use the DNS servers

8.8.8.8 and 8.8.4.4

Just a reminder that applications should always make requests using the api.tfl.gov.uk hostname (via DNS), rather than configured to use specific IP addresses, as these aren’t fixed addresses and your application will break when they change.

1 Like