Basic socket wrapper for converting byte stream into messages for python sockets.
Install using pip:
pip install stream2msg
Initialize sockets:
from stream2msg import SocketWrapper
# Server. Waits for connection.
server = SocketWrapper(is_listener=True, socket_info=('localhost', 5000))
# Client. Assumes server is already running, will raise exception if connection fails.
client = SocketWrapper(is_listener=False, socket_info=('localhost', 5000))
Sending and receiving messages:
from stream2msg import SocketWrapper
# Client sends message.
client.send_data('hello world')
# Server gets message.
print server.get_message()
Note that messages are buffered, i.e. the most recent message is NOT the one that will appear next. For example:
# Client sends message.
client.send_data('hello world')
client.send_data('hello world2')
client.send_data('hello world3')
# Server gets message.
print server.get_message() # Will print ['', 'hello world'].
print server.get_message() # Will print ['', 'hello world2'].
Allows sending of metadata in addition to string messages. Metadata can be sent as an array, and is returned as the first LEN-1 elements of the received message. The string data is always the LEN-1 index of the message.
# Client sends message with metadata.
client.send_data('hello world', ['metadata1', 'metadata2'])
# Server gets message with metadata.
print server.get_message() # Will print ['metadata1', 'metadata2', 'hello world'].
Can be combined with cPickle, json, or other data-to-string converters to easily send any object of choice.
import cPickle as pickle
x = {'object': 'data'}
# Client sends message with metadata.
client.send_data(pickle.dumps(x))
# Server gets message with metadata.
message = server.get_message()
print pickle.loads(message[-1]) # Will print {'object': 'data'}.
Users can set up custom sockets and put them in the wrapper after the fact. It is assumed that the user will handle the initial connection. For example:
import socket
user_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
user_socket.connect(('localhost', 5000))
wrapper = SocketWrapper(s_listener=None, socket_info=None, user_socket=user_socket)
See ExampleClient and ExampleServer for basic echo server implementation.