A Python module that allows you to create and use simple sockets.
Project description
EasySockets
A Python module that allows you to create and use simple sockets.
Installation
The easysockets module can be installed using pip.
pip install easysockets
or
pip install git+https://github.com/Matthias1590/EasySockets.git
You can also install it by cloning this repository and running the following commands:
python3 setup.py build
python3 setup.py install
Examples
The easysockets module contains 3 main classes: ServerSocket
, ClientSocket
and Connection
. Below are examples on how to use each of them.
ServerSocket
:
# The ServerSocket class is used to create a server socket, to create an instance of the ServerSocket class you need a client handler, a client handler is a function that will be passed a connection, the client handler function can then, as the name suggests, handle the client/connection
server_socket = ServerSocket(client_handler=lambda connection: connection.send(b"Hello, world!")) # This handler just sends "Hello, world!" and then closes the connection
# To make the server socket listen, use the "listen" method. You'll have to specify a host and port
server_socket.listen(host="localhost", port=8080)
# Now whenever a client connects, the lambda we made earlier will be called and will send "Hello, world!" to the client after which it'll close the connection
ClientSocket
:
# The ClientSocket class is used to connect to server sockets
client_socket = ClientSocket()
# To connect to a server, use the "connect" method. You'll have to specify the host and port the server is hosted on. This will return a connection that you can then use to communicate with the server
connection = client.connect(host="localhost", port=8080)
# Sending "Hey!" to the server
connection.send(b"Hey!")
# Receiving a message and printing it (NOTE: The message will be received as bytes, to turn it into a string you can use the decode method)
print(connection.receive().decode())
# Closing the connection (NOTE: This isn't necessary if you end the program but it's good practice to do it anyway)
connection.close()
Connection
:
# Assume that we just connected to a server
...
# The Connection class is used to communicate to a server/client, whenever you connect to a server or have a client connect to you, you will be given a Connection instance, you cannot create Connections using the constructor
# Send a message (you can only send bytes, if you want to send a string, just encode it)
connection.send("Hello".encode())
# is the same as
connection.send(b"Hello")
# Receive a message
message = connection.receive()
print(message.decode())
# Closing the connection
connection.close()
For more examples, check out example/client.py
and example/server.py
Downloads
Supported Operating Systems
The easysockets module is supported on every operating system as it only uses the built-in socket
module.
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
File details
Details for the file easysockets-1.0.0.tar.gz
.
File metadata
- Download URL: easysockets-1.0.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de078be1e615224adf3af571c5488f6c3434fca17ae5b692241ef5cc33952390 |
|
MD5 | c95cae4f6033b9765496c4204d119965 |
|
BLAKE2b-256 | c2587edc12583df14169653f2813ef07aa7e9a98f49a66c5cb056898c2f16c82 |