socket_for_humans
A simplified TCP/IP socket setup suitable for many/most lightweight client server programs. The python standard library socket module has a relatively steep learning curve. The socket_for_humans 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 provides two types of objects Server and Connection. The Server object is used to passively listen for new connections, while the Connection object is used to actively initiate a connection and thereafter, once a connection is established, for sending and receiving on both sides of the socket communications.
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.
socket_for_humans is the communications module used in the simple_gossip python gossip protocol.
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 send 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()
See these example files for more usage ideas
Run examples/example_server.py and examples/example_client.py at the same time on same machine to see a 2-way communication on a socket in just a few lines.
Run examples/example_server_b.py and examples/example_client_b.py at the same time on same machine to see the same example with binary bytes data instead of str data.
Metadata
Release files for socket-for-humans 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| socket_for_humans-0.0.3.tar.gz | 7.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| socket_for_humans-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.7 kB
Release files / socket_for_humans-0.0.3.tar.gz
| Download URL | socket_for_humans-0.0.3.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a801aba29963d5eb7af9c9071f376bcebc170c1dbbd6ec0df610b766eaf17c39
|
|
BLAKE2b-256 checksum How to use checksums |
d49724497c71070c9b766c5854dd82d1d0bf94d613d6ff0f9ff215320ac584c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / socket_for_humans-0.0.3-py3-none-any.whl
| Download URL | socket_for_humans-0.0.3-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56fd2db56d751b83a9015b56bc492dccfc66391028286c3461a5fe1d0aba9e21
|
|
BLAKE2b-256 checksum How to use checksums |
6d7642e2887d95524103e2ed825ddbe1645cff32a898df83713bc5f5e964cf42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|