# Discord gateway
Sans-I/O Python implementation of the Discord gateway.
Sans-I/O means that this implements no I/O (network) and operates purely on the
bytes given using `wsproto`.
It means that this implementation can be reused for libraries implemented in a
threading fashion or asyncio/trio/curio.
## Quickstart
Here's a very minimal implementation using the `socket` library and `threading`
for the heartbeating (it does not handle reconnecting or any form of
unexpected disconnections):
```python
import socket
import ssl
import threading
import time
from sys import platform
import certifi
from ez_gateway import DiscordConnection
TOKEN = 'ABC123.XYZ789'
RECV_SIZE = 65536
SERVER_NAME = 'gateway.ez.gg'
def heartbeat(conn, sock):
while True:
sock.send(conn.heartbeat())
time.sleep(conn.heartbeat_interval)
def recv_event(conn, sock):
while True:
for event in conn.events():
return event
for to_send in conn.receive(sock.recv(RECV_SIZE)):
sock.send(to_send)
def main():
# Setup the socket and SSL for the WebSocket Secure connection.
conn = DiscordConnection('gateway.ez.gg', encoding='json')
ctx = ssl.create_default_context(cafile=certifi.where())
sock = socket.create_connection(conn.destination)
sock = ctx.wrap_socket(sock, server_hostname=SERVER_NAME)
sock.send(conn.connect()) # Convert to a WebSocket
# Receive the very first HELLO event.
hello = recv_event(conn, sock)
# Send RESUME or IDENTIFY depending on state (will always be False
# when initially connecting, but may be different when reconnecting).
if conn.should_resume:
sock.send(conn.resume(TOKEN))
else:
sock.send(conn.identify(
token=TOKEN,
intents=65535,
properties={
'$os': platform,
'$browser': 'ez-gateway',
'$device': 'ez-gateway'
},
))
heartbeater = threading.Thread(target=heartbeat, args=(conn,sock))
heartbeater.start()
try:
while True:
event = recv_event(conn, sock)
print('Received:', event)
finally:
sock.shutdown(socket.SHUT_WR)
sock.close()
if __name__ == '__main__':
main()
```
Raw data
{
"_id": null,
"home_page": "",
"name": "ez-gateway",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6.2",
"maintainer_email": "",
"keywords": "discord,discord-api,discord-bot,discord-api-wrapper,ez-gateway,python-3",
"author": "Timo",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/81/96/586ea928ff92e8558885758f5809048cc81b8c093a61527095f536979256/ez_gateway-0.1.0.tar.gz",
"platform": null,
"description": "# Discord gateway\n\nSans-I/O Python implementation of the Discord gateway.\n\nSans-I/O means that this implements no I/O (network) and operates purely on the\nbytes given using `wsproto`.\n\nIt means that this implementation can be reused for libraries implemented in a\nthreading fashion or asyncio/trio/curio.\n\n\n## Quickstart\n\nHere's a very minimal implementation using the `socket` library and `threading`\nfor the heartbeating (it does not handle reconnecting or any form of\nunexpected disconnections):\n\n```python\nimport socket\nimport ssl\nimport threading\nimport time\nfrom sys import platform\n\nimport certifi\nfrom ez_gateway import DiscordConnection\n\n\nTOKEN = 'ABC123.XYZ789'\nRECV_SIZE = 65536\nSERVER_NAME = 'gateway.ez.gg'\n\n\ndef heartbeat(conn, sock):\n while True:\n sock.send(conn.heartbeat())\n time.sleep(conn.heartbeat_interval)\n\n\ndef recv_event(conn, sock):\n while True:\n for event in conn.events():\n return event\n\n for to_send in conn.receive(sock.recv(RECV_SIZE)):\n sock.send(to_send)\n\n\ndef main():\n # Setup the socket and SSL for the WebSocket Secure connection.\n conn = DiscordConnection('gateway.ez.gg', encoding='json')\n ctx = ssl.create_default_context(cafile=certifi.where())\n sock = socket.create_connection(conn.destination)\n sock = ctx.wrap_socket(sock, server_hostname=SERVER_NAME)\n\n sock.send(conn.connect()) # Convert to a WebSocket\n\n # Receive the very first HELLO event.\n hello = recv_event(conn, sock)\n\n # Send RESUME or IDENTIFY depending on state (will always be False\n # when initially connecting, but may be different when reconnecting).\n if conn.should_resume:\n sock.send(conn.resume(TOKEN))\n else:\n sock.send(conn.identify(\n token=TOKEN,\n intents=65535,\n properties={\n '$os': platform,\n '$browser': 'ez-gateway',\n '$device': 'ez-gateway'\n },\n ))\n\n heartbeater = threading.Thread(target=heartbeat, args=(conn,sock))\n heartbeater.start()\n\n try:\n while True:\n event = recv_event(conn, sock)\n print('Received:', event)\n finally:\n sock.shutdown(socket.SHUT_WR)\n sock.close()\n\nif __name__ == '__main__':\n main()\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "Sans-I/O implementation of the Discord gateway",
"version": "0.1.0",
"project_urls": {
"homepage": "https://github.com/Bluenix2/ez-gateway/"
},
"split_keywords": [
"discord",
"discord-api",
"discord-bot",
"discord-api-wrapper",
"ez-gateway",
"python-3"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "aae95d8f66cb8ab9f19597d1f864f31b2c25789f4a9ac5cfc938dac209958dcc",
"md5": "cf8f1d19747048f3c06952beb2df81db",
"sha256": "9be47ac4627ccb170a64f7fe6288fae18af91fc2e55cb7761975c0265d93f9f1"
},
"downloads": -1,
"filename": "ez_gateway-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cf8f1d19747048f3c06952beb2df81db",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6.2",
"size": 9631,
"upload_time": "2024-03-03T22:34:44",
"upload_time_iso_8601": "2024-03-03T22:34:44.842367Z",
"url": "https://files.pythonhosted.org/packages/aa/e9/5d8f66cb8ab9f19597d1f864f31b2c25789f4a9ac5cfc938dac209958dcc/ez_gateway-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8196586ea928ff92e8558885758f5809048cc81b8c093a61527095f536979256",
"md5": "df3b587a25405ae400ed1e2995f242f4",
"sha256": "d69027a9458ced9ea76903b2eeb29ed6d7e24d1f51abe451080a56aefaec563b"
},
"downloads": -1,
"filename": "ez_gateway-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "df3b587a25405ae400ed1e2995f242f4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6.2",
"size": 9492,
"upload_time": "2024-03-03T22:34:48",
"upload_time_iso_8601": "2024-03-03T22:34:48.808219Z",
"url": "https://files.pythonhosted.org/packages/81/96/586ea928ff92e8558885758f5809048cc81b8c093a61527095f536979256/ez_gateway-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-03 22:34:48",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Bluenix2",
"github_project": "ez-gateway",
"github_not_found": true,
"lcname": "ez-gateway"
}