Python - Using socket.gethostbyname through proxy

Viewed 4733

I'm using TOR to proxy connections but am having difficulty proxying DNS lookups via socket.gethostbyname("www.yahoo.com") -- I learned that it was not sending DNS traffic via proxy by sniffing traffic with wireshark. Here's a copy of the code I'm using

import StringIO
import socket
import socks  # SocksiPy module
import stem.process
from stem.util import term

SOCKS_PORT = 7000

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)

socket.socket = socks.socksocket

def getaddrinfo(*args):
    return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo

socket.gethostbyname("www.yahoo.com") <--- This line is not sending traffic via proxy

Any help is greatly appreciated!

2 Answers
Related