1

This is my code:

try:
    r = requests.head(url='http://'+finalURL, headers=headers, timeout=timeout, allow_redirects=True)

    try:
        print('Status', r.status_code)
        print('Headers', r.headers)
        print('Elapsed Total', r.elapsed.total_seconds())

    except NameError as e:
        print("Undefined variable", e)

I have tried this: print(r.raw._original_response.peer) as I saw on another answer on here, but it doesn't work.

Another option is stream=True but in this case Timeout won't work, thats what I read somewhere.

How can I catch the IP and Port?

Nikk
  • 7,384
  • 8
  • 44
  • 90
  • 1
    Possible duplicate of [How do I get the IP address from a http request using the requests library?](https://stackoverflow.com/questions/22492484/how-do-i-get-the-ip-address-from-a-http-request-using-the-requests-library) – Olvin Roght Sep 28 '19 at 19:08
  • 1
    @OlvinRoght Its not a duplicate `r.raw._original_response.peer` doesn't work. This is a `head` not a `get` request! – Nikk Sep 28 '19 at 19:09
  • I don' think it is supported. https://github.com/psf/requests/issues/2158 – Yuvraj Jaiswal Sep 28 '19 at 19:43

1 Answers1

1

If stream=True is acceptable:

response=requests.head('https://stackoverflow.com/questions/58149424/catch-the-ip-and-port-from-a-head-request',stream=True)
print response.raw._connection.sock.getpeername()
('151.101.129.69', 443)
Gabriel E.
  • 51
  • 2
  • Timeout won't work with `stream=True` enabled. It's what I read somewhere. Otherwise it works perfectly. – Nikk Sep 28 '19 at 19:18