A simplified socket setup suitable for many/most lightweight client server programs. The Python standard library TCP/IP socket module has a relatively steep learning curve. This module is intended to help get most projects up and running quickly by wrapping the standard library socket module in an easy to use interface.
Project description
socket_for_humans
A simplified socket setup suitable for many/most lightweight client server programs. The python standard library socket module has a relatively steep learning curve. This module will help get most projects up and running quickly by wrapping the Python standard library socket module in an easy to use interface.
socket_for_humans is the communications module used in the simple_gossip python gossip protocol.
socket_for_humans provides two types of objects Server and Connection. The Server object is used to listen for new connections, while the Connection object is used for sending and receiving on both sides of the socket communications once a connection is established.
By default the send and receive functions take/return str rather than bytes, which is probably what most users initially want, if you want bytes then use str_mode=False in the object constructors.
Usage:
Essential usage follows this pattern:
Example server code:
from socket_for_humans import Server
addr = ('127.0.0.1', 12345)
my_server = Server(addr)
while True:
msg, conn, addr = my_server.get_next()
# msg is the msg recieved by the server.
# conn is a Connection instance used to continue rend and receive with client
# addr is the clients address like (ip, port)
if msg:
# do whatever you need to do with received msg
print(msg)
conn.send("here is my reply")
msg2 = conn.recv()
print(msg2)
conn.send("here is my further reply, if you really want to know")
# continue to use conn to send and receive until the purpose is satisfied.
conn.close()
Example client code:
from socket_for_humans import Connection
addr = ('127.0.0.1', 12345)
my_client = Connection(addr)
msg = "a question to the server"
print("Client sending: " + msg)
my_client.send(msg)
reply = my_client.recv()
print("Client received: " + reply)
msg2 = "a further question to the server"
print("Client sending: " + msg2)
my_client.send(msg2)
reply2 = my_client.recv()
print("Client received: " + reply2)
# continue to use my_client to send and receive until the purpose is satisfied.
my_client.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file socket_for_humans-0.0.2.tar.gz.
File metadata
- Download URL: socket_for_humans-0.0.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea48b33bf017b99a9246de8915d09ce20b13a0c9e8f5cdf63ef2a96895e5db16
|
|
| MD5 |
897b63c7a8c2ff7a768adb168e8ab75a
|
|
| BLAKE2b-256 |
ce15c8740e28f4fbc308e18cdb810f172a16edba9002b9c5483258347b61a287
|
File details
Details for the file socket_for_humans-0.0.2-py3-none-any.whl.
File metadata
- Download URL: socket_for_humans-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0135a6789283d55a38dc6231cb625c990a945feb0e1feeb530ac54153fa13384
|
|
| MD5 |
116261dc8bfc9215d1b1987c6acd157f
|
|
| BLAKE2b-256 |
af65783308cd348a4b7cfbc3bf17922d1fc04fa3f7c34f55fcad026654a952d2
|