import socket
import select
class Socket(object):
""" Create a new socket using the given address family, socket type and protocol number. """
def __init__(self, address, socket_family=socket.AF_INET, socket_type=socket.SOCK_STREAM, proto=0, fileno=None):
self.address = address
self.socket = socket.socket(socket_family, socket_type, proto, fileno)
def connect(self):
"""
Connect to a remote socket at address (host, port).
The format of address depends on the address family.
"""
print("Connecting to: %s:%s" % (self.address[0], self.address[1]))
self.socket.connect(self.address)
def close(self):
"""" Mark the socket closed. """
self.socket.close()
def receive_data(self, max_size=1024):
""" The maximum amount of data to be received at once is specified by max_size. """
return self.socket.recv(max_size)
def send_data(self, data):
""" Send data to the socket. The socket must be connected to a remote socket. """
return self.socket.send(data)
def ready(self):
""" Monitor the file descriptor. """
result = select.select([self.socket], [], [], 0)[0]
return self.socket in result