I am writing a program that needs to redefine how socket.getaddrinfo() works. I use a third party library, which imports several other items, which eventually imports httplib2, which then imports socket.
From what I've read, monkey patching is the way to override this method, but I am obviously not doing it right.
I've tried this my making the function my_getaddrinfo . Right now it just uses the default code
def my_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
addrlist = []
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
af, socktype, proto, canonname, sa = res
addrlist.append((_intenum_converter(af, AddressFamily),_intenum_converter(socktype, SocketKind),proto, canonname, sa))
return addrlist
import socket
socket.getaddrinfo = my_getaddrinfo
#Third party library I'm trying to use
from Otherlibrary.test import testconnector
connector = testconnector(initvalue)
connector.do()
What ends up happening is that I get the error "name _socket not defined". I'm sort of lost here. Any help would be great. Thanks.
Inside your def my_getaddrinfo
, before use _socket
you can do from socket import _socket
and that should do.