【问题标题】:How do I get the IP address from a http request using the requests library ? in python 2.7如何使用 requests 库从 http 请求中获取 IP 地址?在蟒蛇 2.7
【发布时间】:2017-06-26 03:04:52
【问题描述】:

我正在使用 python 中的请求库发出 HTTP 请求,但我需要来自响应 http 请求的服务器的 ip 地址,并且我试图避免进行两次调用(并且可能与响应请求的人。

这可能吗?是否有任何 python http 库允许我这样做?

我也在为请求使用代理

【问题讨论】:

  • 我收到此错误 @LaundroMat 文件“search.py​​”,第 58 行打印 socket.gethostbyname(page) ^ IndentationError: unexpected indent 我正在使用这一行 print socket.gethostbyname(page)内循环
  • 这是一个缩进错误。你可以粘贴你的代码on repl.it 并分享吗?

标签: python-2.7


【解决方案1】:

您可以使用套接字获取 IP 地址 示例:我将使用 Socket + Requests 获取状态码 + ip 地址:

import socket
import requests


url = 'www.google.com'
sock = socket.gethostbyname(url)
url2 = 'http://www.google.com'
request = requests.get(url2)
if request.status_code == 200:
    status = 'HTTP 200 : OK'
elif request.status_code == 302:
    status = 'HTTP 302 : Redirect'
elif request.status_code == 404:
    status = 'HTTP 404 : NOT FOUND'
else:
    status = 'Unknow Status'
print "[+] URL : " + url + "\n"
print "[+] IP Addres : " + sock + "\n"
print "[+] Reponse Status : " + status 

【讨论】:

    猜你喜欢
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 2015-08-31
    • 1970-01-01
    • 2011-07-31
    • 2019-07-28
    相关资源
    最近更新 更多