Python3:如何使用请求正确进行身份验证?尽管使用请求进行身份验证,但仍会打印“ 401需要身份验证”

时间:2020-11-01 00:40:19

标签: python authentication python-requests

我正在尝试从URL下载一些json输出。但是,此URL需要LDAP / AD身份验证。我可能会尝试,但无法验证和下载内容:

#!/bin/python3

import requests, json, urllib3, http.client, pprint
from requests.auth import HTTPBasicAuth

class BlockVolumeStatus():
  getResponse = None
  session = None
  sessionResponse = None
  responseJSON = None

  jmxurl = "jmx-server01.domain.abc"
  jmxurlPort = "1006"
  loginURL = "http://" + jmxurl + ":" + jmxurlPort
  jmxAPI = loginURL + "/jmx/"
  username = "jmx@domain.abc"
  password = "Passw0rd"

  urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  verify="/etc/pki/tls/certs/ca-bundle.trust.crt"

  def GetJMXJson(self):
    self.session = requests.Session()
    response = self.session.head(self.loginURL)
    self.session.headers = response.headers
    self.session.verify = self.verify

    print(self.session.headers)

    try:
      print("loginURL = %s, self.username = %s, self.password = %s " % ( self.loginURL, self.username, self.password ) )
      self.sessionResponse = self.session.get( self.loginURL + "/jmx.html", auth=(self.username,self.password) )
    except requests.exceptions.HTTPError as err:
      raise SystemExit(err)

    print(self.sessionResponse.status_code)
    print(self.sessionResponse.reason)

    if self.sessionResponse.status_code == 200:
        self.getResponse = self.session.get(self.jmxAPI)

        if getResponse.status_code == 200:
            self.responseJSON = agentStatus.json()
        else:
            print(self.getResponse)


    print(self.responseJSON.json())


bvs = BlockVolumeStatus()
bvs.GetJMXJson()

我已经尝试过Google,但似乎无法正确组合请求才能使其正常工作。结果始终如下:

{'Date': 'Sun, 01 Nov 2020 00:09:50 GMT, Sun, 01 Nov 2020 00:09:50 GMT, Sun, 01 Nov 2020 00:09:50 GMT', 'Pragma': 'no-cache, no-cache', 'X-Content-Type-Options': 'nosniff, nosniff', 'X-FRAME-OPTIONS': 'SAMEORIGIN, SAMEORIGIN', 'X-XSS-Protection': '1; mode=block, 1; mode=block', 'WWW-Authenticate': 'Negotiate', 'Set-Cookie': 'hadoop.auth=; Path=/; HttpOnly', 'Cache-Control': 'must-revalidate,no-cache,no-store', 'Content-Type': 'text/html;charset=iso-8859-1', 'Connection': 'close'}
loginURL = http://jmx-server01.domain.abc:1234, self.username = jmx@domain.abc, self.password = Passw0rd
401
Authentication required
Traceback (most recent call last):
  File "./hdfs-jmx.py3", line 52, in <module>
    bvs.GetJMXJson()
  File "./hdfs-jmx.py3", line 48, in GetJMXJson
    print(self.responseJSON.json())
AttributeError: 'NoneType' object has no attribute 'json'

我已经尝试了URL,用户并在Chrome中传递。这有效。为确保我不会误输入任何内容,我将其复制并粘贴到了Chrome登录窗口中,并且一切正常。 Chrome中的响应标头如下:

HTTP/1.1 401 Authentication required
Date: Sat, 31 Oct 2020 22:24:17 GMT
Date: Sat, 31 Oct 2020 22:24:17 GMT
Pragma: no-cache
X-Content-Type-Options: nosniff
X-FRAME-OPTIONS: SAMEORIGIN
X-XSS-Protection: 1; mode=block
WWW-Authenticate: Negotiate
Set-Cookie: hadoop.auth=; Path=/; HttpOnly
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Connection: close

请求标头如下:

GET /jmx.html HTTP/1.1
Host: jmx-server01.domain.abc:1234
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://jmx-server01.domain.abc:1234/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

我要去哪里错了?认为我没有完全理解可能导致我成功的领域之一。

0 个答案:

没有答案