如何在 Windows 上的 Python 中连接到经过 kerberos 身份验证的 REST 服务 [英] How do I connect to a kerberos authenticated REST service in Python on Windows

查看:29
本文介绍了如何在 Windows 上的 Python 中连接到经过 kerberos 身份验证的 REST 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常简单的 Python 脚本来下载我公司内部服务的内容,该服务位于我们的防火墙内并使用 kerberos 进行身份验证.

I am trying to create a very simple Python script to download the contents of an internal service at my company that sits within our firewall and authenticates using kerberos.

当我安装 requests_kerberos 模块时,我首先编辑了其中的 import kerberos 以在安装 kerberos_sspi 后使用 import kerberos_sspi as kerberos模块.

When I installed the requests_kerberos module I first edited the import kerberos in it to use import kerberos_sspi as kerberos instead after having installed the kerberos_sspi module.

因此我有以下 Python 脚本

Thus I have the following Python script

import requests
from requests_kerberos import HTTPKerberosAuth

response = requests.get('http://service.internaldomain',auth=HTTPKerberosAuth())

print response

在尝试处理 401 时,它因错误而崩溃.

While trying to process the 401 it crashes out with the error.

error: (-2146893053, 'InitializeSecurityContext', 'The specified target is unknown or unreachable')

在查看是否可以使用 curl 执行此操作时,我运行了 kinit 并注意到它要求我输入密码以进行以下提示进行授权:

While looking into seeing if I could do this with curl instead I ran kinit and noticed that it asked me for the password to authorisation with the following prompt:

Password for username@additionalInternalDomain.internaldomain

因此我想知道这是否可能是导致问题的原因.

Thus I wondered if this might be what is causing the issue.

推荐答案

我在 python 上尝试了多个库,但在尝试从 Windows 机器进行身份验证时失败了.没有简单的方法.Kerberos 库主要在 Linux 上工作.Windows 的解决方法不起作用.那么有什么办法可以解决这个问题.嗯……在罗马时成为罗马人.尝试使用 Python 中的 Windows 本机库.

I have tried multiple libraries on python and failed when trying to authenticate from a windows machine.There is no easy way. The Kerberos libraries mainly work on Linux. The workarounds for Windows do not work. So what can be the solution to this. Well... be a Roman while in Rome. Try the windows native libraries from Python.

 import sys
 import clr
 from System.Net.Http import *
 myClienthandler = HttpClientHandler()
 myClienthandler.UseDefaultCredentials = True
 myClient = HttpClient(myClienthandler)
 x = myClient.GetStringAsync("putyourURLwithinthequoteshere")
 myresult = x.Result
 print(myresult)

请注意,此 python 脚本必须由有权访问您尝试访问的 URL 的用户运行.通过将 UseDefaultCredentials 属性设置为 True,您可以为登录用户传递 Kerberos 票证.

Note that the this python script will have to run by the user who has access to the URL you are trying to access. By setting UseDefaultCredentials property as True, you are passing the Kerberos tickets for the logged in user.

这篇关于如何在 Windows 上的 Python 中连接到经过 kerberos 身份验证的 REST 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆