01_server.py
We will first create a 01_server.py
file, which starts the websocket server to communicate via message in a separate process.
We will import the Server class from the swergio package as well as the python socket module to retrieve the IP of the current host.
import socket
from swergio import Server
In the file we need to specify the IP and the port of the server as well as the message format and the header length. All of these information have to stay the same across the server and all clients.
# Set server PORT to 8080
PORT = 8080
# Get IP of current host
SERVER = socket.gethostbyname(socket.gethostname())
print(SERVER)
# Using utf-8 as message format
FORMAT = 'utf-8'
# Message header has a maximum length of 10
HEADER_LENGTH = 10
We can then initiate the swergio server class and start the server.
# Initiate the Server class
server = Server(SERVER, PORT, FORMAT, HEADER_LENGTH)
# Start the server
server.start()