I'm trying to create a subclass of socket.socket
class in Python2.7 with overridden send()
and read()
methods so that they dump the data transferred over the socket into the terminal. The code looks like this:
import socket
class SocketMonkey(socket.socket):
def send(self, buf):
print('BUF: {}'.format(buf))
return super(SocketMonkey, self).send(buf)
def recv(self, size=-1):
buf = super(SocketMonkey, self).recv(size)
print('BUF: {}'.format(buf))
return buf
socket.socket = SocketMonkey
And then this is how I instantiate and use this class:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('www.domain.com', 80))
sock.send('GET / HTTP/1.1\n\n')
I've monkey patched socket module, but while socket functionality works as before, data is not being dumped. Any idea where am I going wrong?