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

查看:184
本文介绍了如何在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 以使用 import kerberos_sspi作为kerberos ,而在安装 kerberos_sspi 之后模块.

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天全站免登陆