using urllib.request to query a url with a range of x-api-key headers

Viewed 1691

I am given a url/port and have to use python 3.8 to obtain a response from a server. The "server" requires an "x-api-key" as a password and will not display information without the correct key. (this is at least what I have gotten from it)

The key supposedly is between 5500 and 5600. I am very new to python and wondering why my code is returning nothing. Am I using headers wrong? I've been trying things/googling for several hours.

# Alien Signal API listening on http://127.0.0.1:8082
# Use HTTP GET with x-api-key header to get signal
# We have narrowed down the key to be in the range of 5500 to 5600
# Note: The script can timeout if this occurs try narrowing
# down your search
#
import urllib.parse
import urllib.request

url = "http://127.0.0.1:8082"
startingKey = 5500
endingKey = 5600
hdr = {}

while startingKey <= endingKey:
    hdr = { 'x-api-key' : str(startingKey) }
    req = urllib.request.Request(url, headers=hdr)
    response = urllib.request.urlopen(req)
    response.read()
    startingKey = startingKey + 1
1 Answers

Although you got most of it right, you forgot to print. Just add this line of code instead of "response.read()":

print(response.read())

or you could do this:

html = response.read()
print(html)
Related