【问题标题】:In Python, for a POST request, is it possible to obtain the ip address used for the request from the POST request object?在 Python 中,对于 POST 请求,是否可以从 POST 请求对象中获取用于请求的 IP 地址?
【发布时间】:2018-08-27 02:55:49
【问题描述】:

对于 Python 中的一般 POST 请求,例如:

>>> import requests
>>> r = requests.post(url=url, data=data, headers = headers, cookies = cookies)
>>> type(r)
<class 'requests.models.Response'>

是否可以获取发出请求时使用的 ip 地址?我尝试使用dir(r) 查看其内容并得到:

dir(r)

['attrs', 'bool', 'class', 'delattr', '字典', 'dir', 'doc', 'enter', 'eq', 'exit', '格式', 'ge', 'getattribute', 'getstate', 'gt', 'hash', 'init'、'init_subclass'、'iter'、'le'、'lt', 'module', 'ne', 'new', 'nonzero', 'reduce', 'reduce_ex'、'repr'、'setattr'、'setstate'、 'sizeof', 'str', 'subclasshook', 'weakref', '_content'、'_content_consumed'、'_next'、'apparent_encoding'、 '关闭','连接','内容','cookies','经过','编码', '标题','历史','is_permanent_redirect','is_redirect', 'iter_content', 'iter_lines', 'json', '链接', 'next', 'ok', 'raise_for_status'、'raw'、'reason'、'request'、'status_code'、'text'、 '网址']

这里有什么东西可能包含它吗?谢谢。

【问题讨论】:

    标签: python python-3.x post


    【解决方案1】:

    “不”。实际的 IP 解析发生在 HTTP 请求堆栈中相对较低,并且通常不公开(在“任何”更高级别的 HTTP API 中,无论语言实现如何)。

    取而代之的是get the hostname from the urlresolve the extracted hostname to an IP

    有一个非常简短的计时窗口在技术上“可能”产生不同的值,但它“应该足够好” - 特别是因为网络名称解析被缓存了,并且把它非常依赖已解析的 IP 听起来 .. 很时髦。

    (虽然可以先解析 IP,并将其用于请求,但它也会更改 HTTP 主机标头。所以:可能没用。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-04
      • 2012-08-24
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      相关资源
      最近更新 更多