Python Forum
Listen TCP and send data to websockets
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listen TCP and send data to websockets
#1
Hello,

I have a trouble to do what i want.

I'm not a Python developper...

I have a software who can provide me somme data, but, this software can only send data on a RAW style TCP port in local network.
So i search on internet how to listen data on a specific port (1234) of my computer.
I manage to get the data.
The in another code i try to send test data in my websocket. it is working fine.

My goal now is to send all data to my websocket.
I merge the two code, it work, but i assume i'm not doing it right. Because every time my python script send data to the websocket he open, send and close the websocket.
My code work but after a little time i have a 20 seconds delta between the time i send data and retreive it on my web socket ...
I try to call the websocket after but my code note working ...

There is the code who work at home. But full of problems
(too long, not optimised, and when websocket is off, the script shutdown him sefl and not try to reconnect on it ...)

import socket
import asyncio
import websockets
import json

async def send(client, data):
    await client.send(data)

def server_program():
    host = socket.gethostname()
    port = 1234

    server_socket = socket.socket() 
    server_socket.bind((host, port))

    server_socket.listen(2)
    conn, address = server_socket.accept() 
    print("Connection de: " + str(address))
    while True:        
        data = conn.recv(1024).decode()
        if not data:
            # if data is not received break
            break

        print("Balises: " + str(data))
                
        # Send data to our websocket
        async def hello():
            async with websockets.connect('wss://mywebsocketServer') as websocket:                        
                await websocket.send(json.dumps({"RAW_DATA": str(data) }))    
        asyncio.get_event_loop().run_until_complete(hello()) # this close the websocket communication


    conn.close()  # close the connection

if __name__ == '__main__':
    server_program()


async def send(client, data):
    await client.send(data)
I hope you can help me !
Pray
hi @snippsat i'm sorry i put my message in the wrong section, coul'd you move it in the Network forum ? thank you !
Reply


Messages In This Thread
Listen TCP and send data to websockets - by neoboby - Nov-25-2022, 11:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do i listen to loopback address on my local computer, without a port billykid999 6 6,144 May-29-2023, 07:50 PM
Last Post: billykid999
  Asyncio | Websockets - general problem starting the server dreamer 5 6,626 Oct-26-2022, 11:55 AM
Last Post: dreamer
  websockets help :/ kimblee 3 6,588 Feb-15-2021, 01:26 AM
Last Post: kimblee
  Slow websockets server -> client pattagghiu 0 2,960 Sep-17-2019, 09:28 AM
Last Post: pattagghiu
  Flask and Websockets == True? rxndy 2 3,521 Apr-21-2019, 04:08 PM
Last Post: rxndy
  Send Pressure sensor data to IoT platform using WebSockets in Python barry76 3 5,636 Mar-12-2019, 09:48 AM
Last Post: barry76
  How to send/receive data over TCP Rehan11 1 5,085 Feb-01-2019, 11:21 AM
Last Post: cetpaseo336
  Send data BMP180 between client and server trought module socket smalhao 0 3,378 Jul-30-2018, 12:56 PM
Last Post: smalhao
  UDP Listen without Bind() GaryFunk 3 10,297 Mar-29-2018, 02:28 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020