Skip to main content

module for easy networking

Project description

python-network
==============

Easy tcp, unix socket and avahi networking from python.

This module provides 4 clases for networking support from Python:

Socket
Server

Additionally, there are two convenience functions, bgloop and fgloop, which
will enter the glib main loop. Before that, bgloop will fork, so it returns
control of the terminal to the calling application. When starting servers this
is useful: it will only return after the server is running, so there is no race
condition for "the server was started, but not accepting connections yet".

Instances of the Socket class are the connection objects. Server objects wait
for connections and create the corresponding sockets as calls come in.

Socket addresses can have several types. For connecting (client side), they may
be:

port connect to port on localhost.
host:port connect to port on host.
service| connect to first avahi service with name (strictly,
_service._tcp).
service|regex connect to the first avahi service for which the long name
matches regular expression.
/path/to/socket connect to a unix domain socket. The name must include at least
one forward slash.

For listening (server side), they may be:
port listen on port on localhost.
service|name listen on random port, publish service with long name.
service|name|port listen on port, publish avahi service.
/path/to/socket listen on a unix domain socket, which must not exist.
The name must include at least one forward slash.

In all cases, port may be a number or a service name from /etc/services.

Interface:

s = network.Socket (address, tls = True, disconnect_cb = None)
# Create a socket, connect to address.
# If tls is True, TLS will be used to create a secure socket.
# If disconnect_cb is a function, it is called (with leftover data as
# argument) when the socket is closed. If not set, it will
# raise EOFError when the socket closes.
s.disconnect_cb(cb)
# Set or change disconnect_cb. Set to None to raise EOFError on close.
s.close()
# Close connection.
s.send(data)
# Send a string of data.
data = s.recv(maxsize = 4096)
# Receive at most maxsize bytes.
s.read(callback, maxsize = 4096)
# Let the main loop wait for data, read it, and call the callback when it arrives.
s.rawread(callback)
# Like read, but don't read the data.
s.readlines(callback, maxsize = 4096):
# Like read, but the data is buffered and the callback is called once
# per received line ('\n'-separated).
# The newline is not passed to the callback.
data = s.unread()
# Stop read callback; return any unparsed data from the readlines buffer.

s = network.Server(port, obj, address = '', backlog = 5, tls = None, disconnect_cb = None)
# Start a server. When a new connection is accepted, obj is called with
# the resulting socket. You will normally want obj to be a class, so a
# new object is constructed for each connection. In its constructor,
# you should then set a read callback.
# address is the address to listen on (default everything; it can be
# set to '0.0.0.0' to disable IPv6, to '127.0.0.1' to limit connections
# to localhost, etc.)
# backlog is the number of connections to allow in the queue while the
# server didn't accept them yet.
# If tls is False, the connection will not be encrypted. Otherwise it
# will be; a certificate will be created if there is none in the
# configuration directory (normally ~/.local/share/network/*/).
s.disconnect_cb(cb)
# Set a new disconnect callback.
s.close()
# Stop the server.

Callback signatures:
Socket.read: cb(bytes)
Socket.rawread: cb()
Socket.readlines: cb(str) # does not contain newline.
Server.obj: cb(Socket)

When a socket is disconnected, the remote end calls disconnect_cb if set,
otherwise it raises EOFError. Disconnect_cb can be unset by calling it with
None.

All callbacks (listening, client receiving) are triggered by the glib main
loop, so that must be running to receive the events. {fg,bg}loop() are
provided so programs using this module don't need to import glib itself. Gtk
uses the same main loop, so any gtk program will not need to do anything
special.

The server and avahi code will be disabled if the respective modules are not
available.

Simple example for the servers.
------------------------------------------------------------------------------
#!/usr/bin/env python
# example for network servers.
import network

class Object:
def __init__(self, socket):
self.socket = socket
self.socket.send('Welcome to this server!')
self.socket.readlines(line)
def line(self, l):
print('data received: %s' % l)
self.socket.send('This is a reply.')

server = network.Server(1234, Object)

network.fgloop()
------------------------------------------------------------------------------

And for the clients.
------------------------------------------------------------------------------
#!/usr/bin/env python
# example for network clients.
import network

client = network.Socket('localhost:1234')
client.send('This is data.')
print('The server replied: %s' % client.recv())
------------------------------------------------------------------------------

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

network-wrapper-0.3.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

network_wrapper-0.3-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file network-wrapper-0.3.tar.gz.

File metadata

  • Download URL: network-wrapper-0.3.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for network-wrapper-0.3.tar.gz
Algorithm Hash digest
SHA256 bf60f6db324df532d947a4f6d3d09b450e86a9204f79b2939ba99cddb4e5390d
MD5 431637285154f7c5873de54bd12791f3
BLAKE2b-256 326ff245a3cf03a4d7056cef18125e71951cdbfce3b5620e3486fb7a58d3c715

See more details on using hashes here.

File details

Details for the file network_wrapper-0.3-py3-none-any.whl.

File metadata

  • Download URL: network_wrapper-0.3-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for network_wrapper-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 775cce96567e871cf996f8945e410b82f419c7b15c42e0060bb1e9d9974e442d
MD5 d6b3c148d9705efd48b8482b21a439c4
BLAKE2b-256 4e2b4a5a1ce26935a31aaba78f1881383b64370cbbf774d7d8b7a69fdf330d3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page