Library written in Python 3.7, for easy client-server communication through sockets (TCP)
server.py
& client.py
must be in the same folder of your .py file.
To import all classes from server.py:
from server import *
To import all classes from client.py:
from client import *
There are 3 kinds (classes) of server included in library (server.py
):
Server
IterativeServer
MultithreadedServer
To create a server object (i.e. simple Server
):
server = Server(port, protocol)
or
server = Server(protocol=MyServerProtocol()) # port is by default 1234
Same logic for the other two classes (IterativeServer
, MultithreadedServer
).
listen()
python function establishes connection with client:
server.listen() # server is ready to receive request
To create an object of Client class:
client = Client(host, port, protocol)
or
my_port = 2345
client = Client(port=my_port, protocol=MyClientProtocol()) # host is by default localhost
or
client = Client(protocol=MyClientProtocol()) # host is by default localhost,
# port is by default 1234
open()
function establishes connection with Server (server must probably be listening):
client.open() # client tries to connect with server
# when connected, sends the request
# receives reply
# and closes connection
Server
will wait for a request, will send response (if no errors will occure) and will close the connection. Server
will be used for one and only request.
server = Server(protocol=a_server_protocol()) # create server object (must create a protocol to pass)
server.listen() # server will listen for one request only
IterativeServer
will serve one client at a time, until it is stopped (CTRL + C).
server = IterativeServer(protocol=a_server_protocol()) # create server object (must create a protocol to pass)
server.listen() # server will listen forever, and serve one client each time (not 2 or more parallel)
Multithreaded
server creates a thread, for each request arrives. The difference here (comparing to IterativeServer
) is that our server can serve multiple requests in parallel. Server is active until it is stopped (CTRL + C).
server = MultithreadedServer(protocol=a_server_protocol()) # create server object (must create a protocol to pass)
server.listen() # server will listen forever, and serve multiple client requests in parallel
-
Create a class (i.e.
MyServerProtocol
) & implementServerProtocol
abstract class. -
Implement
process_request()
function (PyCharm screenshot):
- Server knows what to do on request through
process_request()
function.input_msg
is client's request.
class MyServerProtocol(ServerProtocol):
def process_request(self, input_msg):
output = 'Hello Client!' # prints hello to client, when a request arrives
return output
- After finishing your protocol, it is time to integrate it in our server:
if __name__ == '__main__': # server_main.py
server_protocol = MyServerProtocol() # create protocol we written
server = MultiThreadedServer(protocol=server_protocol) # pass it to server (i.e. MultithreadedServer)
server.listen() # server now listening for requests from clients
-
Create a class (i.e.
MyClientProtocol
) & implementClientProtocol
abstract class. -
Implement prepare_request & process_reply function (PyCharm screenshot):
- Client knows what kind of request to send through
prepare_request()
function,process_reply()
function is for printing server's message.input_msg
(process_reply()
parameter) is server's reply.
class MyClientProtocol(ClientProtocol):
def prepare_request(self):
output = 'Hello Server!'
return output
def process_reply(self, input_msg):
print('Response: ', input_msg)
- After finishing writing your protocol, it is time to integrate it in our client object:
if __name__ == '__main__': # client_main.py
client_protocol = MyClientProtocol() # create protocol we written
client = Client(protocol=client_protocol) # pass it to client
client.open() # client will now try to establish connection with server.
# If it does, client will send the request to server
# When a reply comes back, connection closes
Legacy code: Professor K.G. Margaritis