Skip to content

Instantly share code, notes, and snippets.

@shedme
shedme / ws_client.py
Created March 24, 2023 08:54
WebSocket client using asyncio and websockets | Python
import asyncio
import websockets
async def hello():
try:
async with websockets.connect('ws://localhost:8765') as websocket:
# send a message to the server
await websocket.send('Hello, server!')
# receive a message from the server
@shedme
shedme / fernet_test.py
Created March 24, 2023 08:43
Fernet module in to encrypt and decrypt a string | Python
from cryptography.fernet import Fernet
# generate a secret key
#key = Fernet.generate_key()
# get the key from the user
key_input = input("Enter the key: ")
# convert the key input to bytes
key = bytes(key_input, "utf-8")
@shedme
shedme / ws_server.py
Last active March 24, 2023 08:54
WebSocket server using asyncio and websockets | Python
# WebSocket server implementation in Python3 using the websockets library
import asyncio
import websockets
async def server(websocket, path):
print(f"New client connected: {websocket.remote_address}")
async for message in websocket:
print(f"Received message: {message}")
response = f"Server received your message: {message}"
await websocket.send(response)