【问题标题】:how to access sub? urls keeping the main url connection alive如何访问子?使主 url 连接保持活动状态的 url
【发布时间】:2019-12-30 18:02:34
【问题描述】:

试图访问一些子? php urls/endpoints 获取整体硬件健康状态。这里的问题是我只能从浏览器访问那些带有长 cookie 的 url/端点。当我尝试使用 python 和 linux cmd 行时出现 401 错误

url_login_page=https://10.10.10.10/designs/imm/index.php

下面是我想用 python 和 curl 访问的可用 php,并且能够从浏览器中毫无问题地查看这些 php 的内容

{"home.php":"1"},{"event-log.php":"4"},{"event-notification.php":"5"},
{"event-notification.php":"5"},{"service-problems.php":"6"},{"servicesettings.php":"7"},{"download-service-data.php":"8"},{"serverfirmware.php":"9"},{"remote-control.php":"10"},
{"server-properties.php":"11"},{"server-actions.php":"12"},{"fanlist.php":"13"},
{"power-modules.php":"14"},{"disk-list.php":"15"},{"memorylist.php":"16"},
{"cpu-list.php":"17"},{"server-timeouts.php":"18"},{"immproperties.php":"19"},
{"user-list.php":"20"},{"network-properties.php":"21"},{"immsecurity.php":"22"},
{"backup-restore.php":"23"},{"pxe-network-boot.php":"28"},{"osfailurescreen.php":"29"},{"feature-on-demand.php":"80"}]

更新

现在我可以通过请求获取 cookie 值和标头,

import requests

>>> headers = {
    'Sec-Fetch-Mode': 'cors',
    'Referer': 'https://10.10.10.10/designs/imm/index.php',
    'Origin': 'https://10.10.10.10',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded',
}

>>> data = {
  'user': 'user',
  'password': 'pass',
  'SessionTimeout': '1200'
}

>>> response = requests.post('https://10.10.10.10/data/login', headers=headers, data=data)



>>> response.text
u'{"status":"ok","authResult":"0","TimeWait":"0","iteSrc":"0","forwardUrl":"index-console.php",
"redirection":"home.php","token1_name":"ST17A1FA74B","token1_value":"FF7A4A9934170D1E",
"token2_name":"ST25220A3EA","token2_value":"A547237AA8EB5709",
"token3_name":"ST3FD5CF3C9","token3_value":"C97E95BB2097CA95","errorMsg":""}'


>>> response.headers
{'Content-length': '318', 'X-XSS-Protection': '1; mode=block', 'Content-Security-Policy': "default-src 'self';
connect-src 'self' ws://*:3900/ wss://*:3900/; script-src 'self' 'unsafe-inline' 'unsafe-eval';
style-src 'self' 'unsafe-inline'; font-src 'self' data:", 'X-Content-Type-Options': 'nosniff', 
'Set-Cookie': '_appwebSessionId_=24eb46d3ec26bef7efc9b754f8c79548; 
 path=/; httponly; secure', 'Strict-Transport-Security': 'max-age=31536000;', 
'Keep-Alive': 'timeout=60, max=2000',
'Connection': 'keep-alive', 'Pragma': 'no-cache', 
'Cache-control': 'no-cache="set-cookie"',
'X-Frame-Options': 'SAMEORIGIN', 'Content-type': 'text/json'}


response.cookies
<RequestsCookieJar[Cookie(version=0, name='_appwebSessionId_', value='24eb46d3ec26bef7efc9b754f8c79548',
port=None, port_specified=False, domain='10.10.10.10', domain_specified=False, 
domain_initial_dot=False, path='/', path_specified=True, 
secure=True, expires=None, discard=True, 
comment=None, comment_url=None, rest={'httponly': None}, rfc2109=False)]>

问题是我尝试使用令牌值和会话 ID 来访问子 url 并得到 401 错误。

url1 = 'https://10.10.10.10/designs/imm/dataproviders/imm_status_hardware.php'
>>> resp = requests.post(url1, headers=response.headers, cookies=response.cookies, data=data, verify=False)
>>> resp
<Response [401]>
>>>

我确定没有以正确的方式尝试标头和 cookie

请帮助我如何访问子网址?

【问题讨论】:

  • 如果您列出了相关网站在浏览器上存储的所有 cookie,将会很有用。
  • 能否请您在此处提供 r.headers ?
  • 您是否获得并使用了会话?
  • 我更新了新发现

标签: python linux curl cookies http-headers


【解决方案1】:

您尝试过sessions with the Request 库吗? 我记得曾在与您类似的场景中使用它来保留一个会话,在该会话中我发出了多个请求,其中后续请求需要存在某些 cookie,否则它们会失败。

【讨论】:

  • 我更新了我的发现,当使用令牌/会话 ID 访问子 url 时,能够获得标头和 cookie 的响应,最终出现 401 错误
【解决方案2】:

您已收到 JSON 格式的响应。只需使用 Python 中的 json 库将其提取出来。

import json

然后通过以下方式提取Request r:

data = json.loads(r.text)

数据变量将包含请求提取的所有元组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    相关资源
    最近更新 更多