A cross platform networking library written in Python
Project description
Data Transfer Protocol Library
dtplib is a cross platform networking library written in Python.
Source: https://github.com/WKHAllen/dtplib
Dependencies
Installation
$ pip install dtplib
Server
Creating a server
s = Server(onRecv=None, onConnect=None, onDisconnect=None, blocking=False, eventBlocking=False, recvDir=None, daemon=True, jsonEncode=False)
Create a server object.
onRecv
is a function that will be called when a packet is received. It takes the following parameters: client socket, data, datatype (0: object, 1: file).
onConnect
is a function that will be called when a client connects. It takes the following parameters: client socket.
onDisconnect
is a function that will be called when a client disconnects. It takes the following parameters: client socket.
If blocking
is True, the start method will block until stopping.
If eventBlocking
is True, onRecv
, onConnect
, and onDisconnect
will block when called.
recvDir
is the directory in which files will be put in when received.
If daemon
is True, all threads spawned will be daemon threads.
If jsonEncode
is True, packets will be encoded using json instad of pickle.
Starting the server
s.start(host=None, port=None)
Start the server. If host
is not specified, it will default to socket.gethostname()
. If port
is not specified, it will default to 0
, which represents an arbitrary unused port.
Stopping the server
s.stop()
Stop the server.
Check if the server is serving
s.serving()
Get the server's address
s.getAddr()
Get a client's address
s.getClientAddr(conn)
Get a client's address, providing the client socket object.
Get a list of a server's clients
s.getClients()
Remove a client
s.removeClient(conn)
Disconnect a client from the server, providing the client socket object.
Send data to a client
s.send(data, conn=None)
Send data to a client. conn
is the client socket object to send the data to. If conn
is None, data is sent to all clients.
Send a file or directory to a client
s.sendFile(path, conn=None)
Send a file or directory to a client. path
is the path to the file or directory. conn
is the client socket object to send the file or directory to. If conn
is None, the file or directory is sent to all clients.
Server alternative
The dtplib server object can be used in a with statement.
with server(host, port, *args, **kwargs) as s:
pass
host
is the server IP address. port
is the port number. args
and kwargs
will be passed to the Server class constructor.
Client
Creating a client
c = Client(onRecv=None, onDisconnected=None, blocking=False, eventBlocking=False, recvDir=None, daemon=True, jsonEncode=False)
Create a client object.
onRecv
is a function that will be called when a packet is received. It takes the following parameters: data, datatype (0: object, 1: file).
onDisconnected
is a function that will be called when the server disconnects suddenly. It takes no parameters.
If blocking
is True, the connect method will block until disconnecting.
If eventBlocking
is True, onRecv
and onDisconnected
will block when called.
recvDir
is the directory in which files will be put in when received.
If daemon
is True, all threads spawned will be daemon threads.
If jsonEncode
is True, packets will be encoded using json instad of pickle.
Connecting to a server
c.connect(host, port)
Disconnecting from the server
c.disconnect()
Check if connected to a server
c.connected()
Get the client's address
c.getAddr()
Get the remote server's address
c.getServerAddr()
Send data to the server
c.send(data)
Send data to the server, providing the data to be sent.
Send a file or directory to the server
c.sendFile(path)
Send a file or directory to the server, providing the path to the file or directory to be sent.
Client alternative
The dtplib client object can be used in a with statement.
with client(host, port, *args, **kwargs) as c:
pass
host
is the server IP address. port
is the port number. args
and kwargs
will be passed to the Client class constructor.
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 Distributions
Built Distribution
File details
Details for the file dtplib-1.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: dtplib-1.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b7c0d73d4f19fa43668b142bdca67363d9578b95590716370d23637a259f939 |
|
MD5 | 4601d0701a7a527b3bbf2e226bb7468a |
|
BLAKE2b-256 | a77da53a111a530f54612016b384a36ce4e62ab65a703fb72adc24270c440154 |