gpt4 book ai didi

Python 请求发送前查看

转载 作者:行者123 更新时间:2023-12-02 05:41:52 30 4
gpt4 key购买 nike

有人可以为我提供一种方法,在将请求发送到服务器之前查看我生成的请求吗?代码如下:

import requests
import urllib2
import logging

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

url = "https://10.1.1.254:4081/admin/api/jsonrpc/"
session = requests.Session()

data = {
"jsonrpc": "2.0", "id": 1, "method": "Session.login",
"params": {"userName": "test", "password":"test123"}
}
r = session.post(url, json=data, verify=False)

data = {"jsonrpc": "2.0", "id":3, "method":"Session.logout"}
r = session.post(url, json=data, verify=False)

所以我想要的是在 Python 发送请求之前使用 session.post 发送该请求。

最佳答案

这里有三个选项:

  • 通过使用准备好的请求,
  • 发送请求后检查请求,
  • 您可以启用登录 urllib3以及在 http.client 中启用调试图书馆。

它们为您提供了对输出内容的不同级别的控制。

准备好的请求

您可以prepare a request ,这将为您提供一个新对象,其中所有数据均以最终形式设置。你可以看看 requests.PreparedRequest API docs查看属性的完整列表。

下面是一个示例函数,它简单地格式化数据,就像通过网络发送数据一样(除了它实际上不使用 CRLF 行分隔符,并用占位符字符串替换二进制内容):

def format_prepped_request(prepped, encoding=None):
# prepped has .method, .path_url, .headers and .body attribute to view the request
encoding = encoding or requests.utils.get_encoding_from_headers(prepped.headers)
body = prepped.body.decode(encoding) if encoding else '<binary data>'
headers = '\n'.join(['{}: {}'.format(*hv) for hv in prepped.headers.items()])
return f"""\
{prepped.method} {prepped.path_url} HTTP/1.1
{headers}

{body}"""

使用准备好的请求而不是使用session.<em>[httpmethod]</em>() ,而是使用 session.send() 检查后发送准备好的请求。将 HTTP 方法作为 Request() 的第一个参数相反(大写)。所以session.post(...)变成Request('POST', ...) 。所有其他参数,除了 verify 移至 Request()调用:

url = "https://10.1.1.254:4081/admin/api/jsonrpc/"
session = requests.Session()

data = {
"jsonrpc": "2.0", "id":1, "method": "Session.login",
"params": {"userName": "test", "password":"test123"}
}

request = requests.Request('POST', url, json=data)
prepped = session.prepare_request(request)

print("Sending request:")
print(format_prepped_request(prepped, 'utf8'))
print()
r = session.send(prepped, verify=False)

data = {"jsonrpc": "2.0", "id":3, "method":"Session.logout"}
request = requests.Request('POST', url, json=data)
prepped = session.prepare_request(request)

print("Sending request:")
print(format_prepped_request(prepped, 'utf8'))
print()
r = session.send(prepped, verify=False)

对于您的示例请求,此输出:

Sending request:
POST /admin/api/jsonrpc/ HTTP/1.1
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 109
Content-Type: application/json

{"jsonrpc": "2.0", "id": 1, "method": "Session.login", "params": {"userName": "test", "password": "test123"}}

Sending request:
POST /admin/api/jsonrpc/ HTTP/1.1
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 55
Content-Type: application/json

{"jsonrpc": "2.0", "id": 3, "method": "Session.logout"}

发送后检查请求

响应对象引用用于发出请求的准备好的请求对象,因此您可以在发送请求后打印此:

# ...
r = session.post(url, json=data, verify=False)
prepped = r.request
print("Request that was sent:")
print(format_prepped_request(prepped, 'utf8'))
print()

这当然不完全相同,请求现在已经发送。另一方面,您不需要直接使用准备好的请求,session.post()完成了生成该对象本身的步骤。

请注意,如果您的原始请求导致重定向,则最终请求可能会有所不同。看 response.history attribute访问任何先前的响应(每个响应都附加了自己的请求)。

记录

使用 logging module ,以及装配 http.client.HTTPConnection类将其调试输出记录到同一框架,您也可以获得请求的调试级别信息。我正在使用我对 Log all requests from the python-requests module 的回答这里:

import logging

# function definition from https://stackoverflow.com/a/16337639/100297
# configures http.client to log rather than print
httpclient_logging_patch()
logging.basicConfig(level=logging.DEBUG)

此时您将看到日志输出中出现请求:

DEBUG:http.client:send: b'POST /admin/api/jsonrpc/ HTTP/1.1\r\nHost: 10.1.1.254:4081\r\nUser-Agent: python-requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 109\r\nContent-Type: application/json\r\n\r\n'
DEBUG:http.client:send: b'{"jsonrpc": "2.0", "id": 1, "method": "Session.login", "params": {"userName": "test", "password": "test123"}}'
DEBUG:http.client:reply: 'HTTP/1.1 200 OK\r\n'
DEBUG:http.client:header: Date: Tue, 04 Feb 2020 13:41:52 GMT
DEBUG:http.client:header: Content-Type: application/json
DEBUG:http.client:header: Content-Length: 574
DEBUG:http.client:header: Connection: keep-alive
DEBUG:http.client:header: Server: gunicorn/19.9.0
DEBUG:http.client:header: Access-Control-Allow-Origin: *
DEBUG:http.client:header: Access-Control-Allow-Credentials: true
DEBUG:urllib3.connectionpool:https://10.1.1.254:4081 "POST /admin/api/jsonrpc/ HTTP/1.1" 200 574
DEBUG:http.client:send: b'POST /admin/api/jsonrpc/ HTTP/1.1\r\nHost: 10.1.1.254:4081\r\nUser-Agent: python-requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 55\r\nContent-Type: application/json\r\n\r\n'
DEBUG:http.client:send: b'{"jsonrpc": "2.0", "id": 3, "method": "Session.logout"}'
DEBUG:http.client:reply: 'HTTP/1.1 200 OK\r\n'
DEBUG:http.client:header: Date: Tue, 04 Feb 2020 13:43:56 GMT
DEBUG:http.client:header: Content-Type: application/json
DEBUG:http.client:header: Content-Length: 574
DEBUG:http.client:header: Connection: keep-alive
DEBUG:http.client:header: Server: gunicorn/19.9.0
DEBUG:http.client:header: Access-Control-Allow-Origin: *
DEBUG:http.client:header: Access-Control-Allow-Credentials: true
DEBUG:urllib3.connectionpool:https://10.1.1.254:4081 "POST /admin/api/jsonrpc/ HTTP/1.1" 200 574

这不像使用准备好的请求那么灵活,但可以与现有的代码库一起使用。它还输出响应的数据,但您可以对请求提供的响应对象执行相同的操作,并将其打印出来,就像我们对上面准备好的请求所做的那样。

关于Python 请求发送前查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37453423/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com