0

I am trying to use Python requests to make a HTTP GET request to a domain, without using urllib3/httplib.HTTPConnection to perform a DNS request for the domain. I set the domain in the Windows hosts file, but Python requests appears to override this, so I need to define the DNS resolution for the domain in the script.

I want to script to bypass the dns request so I can set the IP address. In the example below I've set this to 45.22.67.8, and I will change this to my public IP address later.

I tried using this 'monkey patching' technique but it doesn't work. Requests doesn't generate a DNS request in Wireshark, but it also doesn't connect to the HTTP server.

    import socket
    import requests
    from requests.packages.urllib3.connection import HTTPConnection

    socket.getaddrinfo = '45.22.67.8'       

     url = "http://www.randomdomain.com"
     requests.get(url, timeout=10)

Error

'str' object is not callable

Thanks!

Edit: just updated the code in my example. All I want to do is override future http connections to trick the http packets to go to a different destination IP.

mbudge
  • 557
  • 13
  • 28
  • This appears to be a problem with your `prv_getaddrinfo(*args)` call... but you don't show us what those args are. Can you duplicate the problem with a one-liner that shows the `getaddrinfo` call failing? It could be you don't use the right name or port. – tdelaney Apr 30 '16 at 10:22
  • Why don't you directly use the IP address, ie `url = "http://45.22.67.8"` ? Assuming of course it will stay fixed (which you already seem to assume by hardcoding it in the code) – DeepSpace Apr 30 '16 at 10:22
  • Im not sure, i just copied code from this post http://stackoverflow.com/questions/2236498/tell-urllib2-to-use-custom-dns Unfortunately for this project I can't do direct to IP connections, as I have to send a http packet over the network to a domain, but change the destination server to one I control. – mbudge Apr 30 '16 at 11:40
  • Found what I was looking for here > http://stackoverflow.com/questions/2236498/tell-urllib2-to-use-custom-dns – mbudge Apr 30 '16 at 20:37

0 Answers0