Library to simplify working with socket
Project description
Welcome to PySocketLib quick start
You will create simple echo-server
Creating project
Create files with following structure:
.
├── Client
│ └── Main.py
└── Server
└── Main.py
Server
First, create class EchoServer
, which is overloading TCPServer
:
from PySocketLib.Server import TCPServer
from PySocketLib.Client import ConnectedClient
import socket
class EchoServer(TCPServer):
def connect_client(self):
...
def disconnect_client(self):
...
def on_receive(self):
...
def on_send(self):
...
if __name__ == '__main__':
server = EchoServer(port=5678)
while True:
server.proceed()
Methods:
connect_client
- Actions to do when client connects,
disconnect_client
- Actions to do when client disconnects,
on_receive
- Actions to do when server receives message from client,
on_send
- Actions to do when server sends message to client,
Added some functionality:
from PySocketLib.Server import TCPServer
from PySocketLib.Client import ConnectedClient
import socket
class EchoServer(TCPServer):
client_messages = dict()
def connect_client(self, sock: socket.socket) -> ConnectedClient:
new_client = super().connect_client(sock)
print(f'[INFO] Connected client with address: {new_client.addr}')
def disconnect_client(self, client: ConnectedClient):
print(f'[INFO] Disconnect client with address: {client.addr}')
return super().disconnect_client(client)
def on_receive(self, data: bytes, client: ConnectedClient):
self.client_messages[client] = data
print(f'[INFO] Received {data}')
return super().on_receive(data, client)
def on_send(self, client: ConnectedClient):
message = self.client_messages.get(client)
if (message != None):
print(f'[INFO] Sent data {message}')
self.client_messages[client] = None
return message
else:
return b''
if __name__ == '__main__':
server = EchoServer(port=5678)
while True:
server.proceed()
Client
Let's create simple client, which input something from user and send it to server:
from PySocketLib.Client import Client
class MyClient(Client):
def send(self, data: bytes):
data = input('You: ')
return super().send(data)
def on_receive(self, data: bytes):
print('Server: ' + str(data))
return super().on_receive(data)
if __name__ == '__main__':
cl = MyClient(hostname='127.0.0.1', port=5678)
while True:
cl.send(b'')
cl.proceed()
Result
Server output:
[INFO] Connected client with address: ('127.0.0.1', 39652)
[INFO] Received b'Test message'
[INFO] Sent data b'Test message'
[INFO] Received b'1234'
[INFO] Sent data b'1234'
[INFO] Received b'Hi'
[INFO] Sent data b'Hi'
[INFO] Disconnect client with address: ('127.0.0.1', 39652)
^C
Client output:
You: Test message
Server: b'Test message'
You: 1234
Server: b'1234'
You: Hi
Server: b'Hi'
You: ^C
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
PySocketLib-0.1.1.tar.gz
(5.6 kB
view hashes)
Built Distribution
Close
Hashes for PySocketLib-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4866ce5888cbf7a195720cb91fdaa23465fc06ff139be5367a6581dd958ecc66 |
|
MD5 | 848aee07345821058ef44a9193c0ff97 |
|
BLAKE2b-256 | e7511f498986cd48bf5063ebfea21b35132645bf1b2b5143cd6d6a386296ea4a |