Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基本身份验证不适用于请求库_Python_Api_Python Requests_Appdynamics - Fatal编程技术网

Python 基本身份验证不适用于请求库

Python 基本身份验证不适用于请求库,python,api,python-requests,appdynamics,Python,Api,Python Requests,Appdynamics,我正在尝试在python中使用basic auth auth = requests.post('http://' + hostname, auth=HTTPBasicAuth(user, password)) request = requests.get('http://' + hostname + '/rest/applications') 响应表单auth变量: <<class 'requests.cookies.RequestsCookieJar'>[<Cooki

我正在尝试在python中使用basic auth

auth = requests.post('http://' + hostname, auth=HTTPBasicAuth(user, password))
request = requests.get('http://' + hostname + '/rest/applications')
响应表单auth变量:

<<class 'requests.cookies.RequestsCookieJar'>[<Cookie JSESSIONID=cb10906c6219c07f887dff5312fb for appdynamics/controller>]>
200
CaseInsensitiveDict({'content-encoding': 'gzip', 'x-powered-by': 'JSP/2.2', 'transfer-encoding': 'chunked', 'set-cookie': 'JSESSIONID=cb10906c6219c07f887dff5312fb; Path=/controller; HttpOnly', 'expires': 'Wed, 05 Nov 2014 19:03:37 GMT', 'server': 'nginx/1.1.19', 'connection': 'keep-alive', 'pragma': 'no-cache', 'cache-control': 'max-age=78000', 'date': 'Tue, 04 Nov 2014 21:23:37 GMT', 'content-type': 'text/html;charset=ISO-8859-1'})

200
案件不敏感医学({'content-encoding':'gzip','x-powered-by':'JSP/2.2','transfer-encoding':'chunked','set-cookie':'JSESSIONID=cb10906c621C07F887DFF5312FB;Path=/controller;HttpOnly','expires':'Wed,2014年11月5日19:03:37 GMT','server':'nginx/1.1.19','connection','keep-alive','pragma':'no-cache','cache-control','max-age=78000','date:'Tue,11月2日014 21:23:37 GMT,“内容类型”:text/html;charset=ISO-8859-1'})
但当我试图从不同的位置获取数据时,我得到了401错误

<<class 'requests.cookies.RequestsCookieJar'>[]>
401
CaseInsensitiveDict({'content-length': '1073', 'x-powered-by': 'Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)', 'expires': 'Thu, 01 Jan 1970 00:00:00 UTC', 'server': 'nginx/1.1.19', 'connection': 'keep-alive', 'pragma': 'No-cache', 'cache-control': 'no-cache', 'date': 'Tue, 04 Nov 2014 21:23:37 GMT', 'content-type': 'text/html', 'www-authenticate': 'Basic realm="controller_realm"'})

401
不区分大小写的指令({'content-length':'1073','x-powered-by':'Servlet/3.0 JSP/2.2(GlassFish Server开源版3.1.2.2 Java/Oracle Corporation/1.7)“,”过期“:”1970年1月1日星期四00:00:00 UTC“,”服务器“:”nginx/1.1.19“,”连接“:”保持活动“,”pragma“:”无缓存“,”缓存控制“:”无缓存“,”日期“:”2014年11月4日星期二21:23:37 GMT“,”内容类型“:”文本/html“,”www-authenticate“:”基本领域=“控制器\领域“,”
据我所知,在第二个请求中,会话参数不是被替换的。

您需要使用一个会话,并在每个请求中发送身份验证。会话还将为您跟踪cookies:

session = requests.Session()
session.auth = (user, password)

auth = session.post('http://' + hostname)
response = session.get('http://' + hostname + '/rest/applications')
注意:有时,我们可能会得到SSL错误证书验证失败,为了避免,我们可以对python 2使用
verify=False

base64string=base64.encodestring(“%s:%s”“(用户名,密码))。替换('\n',“”)
request=urlib2.request(url)
请求。添加_头(“授权”,“基本%s”%base64string)
结果=urlib2.urlopen(请求)
data=result.read()

在Python3中,它变得很简单:

import requests
response = requests.get(uri, auth=(user, password))

以下几点对我有用

from requests.auth导入HTTPDigestAuth
url='1〕https://someserver.com'
get(url,auth=HTTPDigestAuth('user','pass'))

您需要包含第二次请求的代码我不知道如何做,也找不到合适的手册我的意思是,您需要包含第二次请求的代码。第二次请求指的是“获取”请求吗?谢谢您的回复,但它也不起作用:(错误与previous@oleksii:定义“不工作”。当您使用POST时(我注意到没有参数),服务器是否返回您需要用于REST API的任何令牌或任何东西?换句话说,API文档说明您需要做什么?@Sarit怎么做?请注意,我在回答中链接到了官方文档。@MartijnPieters在阅读了您的第10页评论后。我仍然不明白。我可以在P中使用basicAuthostman.Postman会将用户名/密码分解为标题
Authorization:Basic
。但我无法连接为什么我需要这里的会话,但它能工作!@oleksii-trunt有什么问题?@Sarit:标题需要包含在发送给服务器的每个请求中;通常服务器可以根据在所有的请求中添加头的最简单的方法是使用会话。您不必这样做,但是必须在每个请求中手动包含标题。为什么验证失败?浏览器访问显示证书被验证。考虑避免<代码>验证= false < /代码>,因为它使应用程序容易受到MITM A的影响。ttacks on https协议。我投票赞成使用
HTTPBasicAuth
作为身份验证,这是我一直在寻找的。我不会在生产中使用
verify=False
requests.posts
对我不起作用,但是
requests.get
可以,还有一个较短的版本
requests.get(url,auth)=(用户名、密码))
import requests
response = requests.get(uri, auth=(user, password))