Catégories
CRYPTOCURRENCY

Ethereum: Sharing data between binance twisted.internet websocket and asyncio thread

Here is an example article on Clating and Websocket Connection between Twisted Websocket Binance and Asyncio thread:

Ethereum: Sharing data between Twisted International Websocket and Asyncio Thread

As the popularity of Ethereum increases, as well as the demand for reliable and effective ways of interaction. In the example of SOUS there is creny a websocket connection using Twisted International Binance and the thread asyncio in Python.

In this article, we will tell you how to find a websocket from Binance, which is taken in the API dates.

preliminary requirements

Ethereum: Sharing data between binance twisted.internet websocket and asyncio thread

*

Before we dive in the code, you have.

  • Python 3.6+

  • Twisted International Websocket Library (`Twisted) binance

  • « Asyncio » library

Code*

Here is an example of TEP-By-Step to create a Websocket service on Binance, which downloads data, API and sends it toyncio.

`Python

Earlier binance.websockets Import binanceCketmanager

Import asyncio

Import json

Define URL for the API Binance interface (novel it with the API key)

Binance_api_url = "

Class Websocketserver:

Def __init __ (me):

Self.socket_manager = binencesocketmanager (binance_api_url, on_open = self.on_open)

Async Def on_Open (self, nest):

Wait for Socket.Send ("Connected")

ASync Def Handle_Message (self, message):

print (f "Message received: {message}")

Async def main ():

Create a websocket server

Ws_server = websocketserver ()

Define the Ensincio Ent loop

Coro = asyncio.get_vent_loop ()

Start asynchronous task to send ASYNCIO data

Async def sen_data_to_thread ():

That's true:

Message = wait for ws_server.socket_manager.recv ()

If the news.startswith ("disconnect"):

break

Pars and process the received message

Data = Json.LADS (Message)

print (f "received data: {data}")

Simulate processing time

Wait for asyncio.sleep (1)

Start asynchronous task to send ASYNCIO data

coro.run_until_compete (send_data_to_thread ())

asyncio.get_event_loop () run_until_compete (Main ()))

Explanation*

In this code, we define the « websocketserver » class, which establishes a connection with the international use of binance when receiving the upcoming message, we print it on the console.

We are asyncio asyncio and we land two asynchronous tasks: one to send to the same way as processes. Send_data_to_thread functions are time for time by sleeping

Finally, we deal with both tasks simultaneously using `coro.run_until_compete (send_data_to_thread ()).

Application*

In this article, we showed how to create a websocket connection between Twisted International International and Websocket Library and Ayncio. By following the above example of code, you can get a WebSocket service to undertake processes.

Note that this is just a basic example that you can start. You can modify the code to adapt to CSE and support errors more strongly.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *