Examples¶
The directory examples
in
SlipLib’s GitHub repository
contains some basic examples on how the sliplib
module can be used.
Echoserver¶
This directory contains an example server and client application that demonstrate a basic use-case for Slip-encoded messages. The example works both for IPv4 and IPv6 sockets.
Server¶
The server.py
example file is a demonstrator echo server.
It uses a subclass of SlipRequestHandler
that transforms the request
attribute into
a dedicated socket subclass that prints
the raw data that is received and sent.
The request handler prints the decoded message,
and then reverses the order of the bytes in the encoded message
(so abc
becomes cab
),
and sends it back to the client.
Client¶
The client.py
example file is a client for the demonstrator echo server .
It prompts the user for a message,
encodes it in a Slip packet, sends it to the server,
and prints the decoded reply it gets back from the server.
This is repeated until the user enters an empty message.
Usage¶
Open a terminal window in the echoserver
directory
and run the server_ipv6.py
script. This will start the server
and print the address on which the server is listening.
$ python server.py
Slip server listening on localhost, port 59454
Then in another terminal window in the same directory run the client.py
script
with the port number reported by the server.
$ python client.py 59454
Connecting to server on port 59454
Connected to ('127.0.0.1', 59454)
Message>
You can now enter a message, and the client will print the response from the server before prompting for the next message. An empty message stops both the client and the server.
$ python client.py 59454
Connecting to server on port 59454
Connected to ('127.0.0.1', 59454)
Message> hallo
Response: b'ollah'
Message> bye
Response: b'eyb'
Message>
$
The server will have printed the following information:
$ python server_ipv6.py
Slip server listening on localhost, port 59454
Incoming connection from ('127.0.0.1', 59458)
Raw data received: b'\xc0hallo\xc0'
Decoded data: b'hallo'
Sending raw data: b'\xc0ollah\xc0'
Raw data received: b'\xc0bye\xc0'
Decoded data: b'bye'
Sending raw data: b'\xc0eyb\xc0'
Raw data received: b''
Decoded data: b''
Closing down
$
Running on IPv6¶
By running the server with the argument ipv6
,
an IPv6-based connection will be established.
In the server terminal window:
$ python server.py ipv6
Slip server listening on localhost, port 59454
Incoming connection from ('::1', 59458, 0, 0)
...
In the client terminal window:
$ python client.py 59454
Connecting to server on port 59454
Connected to ('::1', 59454, 0, 0)
Message>
...