使用 urllib.request 查询范围为 x-api-key headers 的 url
using urllib.request to query a url with a range of x-api-key headers
我得到了 url/port 并且必须使用 python 3.8 从服务器获取响应。 “服务器”需要“x-api-key”作为密码,如果没有正确的密钥将不会显示信息。 (这至少是我从中得到的)
密钥应该在 5500 和 5600 之间。
我是 python 的新手,想知道为什么我的代码没有返回任何内容。
我使用 headers 错了吗?
我已经尝试 things/googling 几个小时了。
# 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
虽然你大部分都答对了,但你忘了打印。只需添加这行代码而不是 "response.read()":
print(response.read())
或者您可以这样做:
html = response.read()
print(html)
我得到了 url/port 并且必须使用 python 3.8 从服务器获取响应。 “服务器”需要“x-api-key”作为密码,如果没有正确的密钥将不会显示信息。 (这至少是我从中得到的)
密钥应该在 5500 和 5600 之间。 我是 python 的新手,想知道为什么我的代码没有返回任何内容。 我使用 headers 错了吗? 我已经尝试 things/googling 几个小时了。
# 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
虽然你大部分都答对了,但你忘了打印。只需添加这行代码而不是 "response.read()":
print(response.read())
或者您可以这样做:
html = response.read()
print(html)