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
#2
Hi!

I found my way ! thank's

import socket
import websocket
import json

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))
    
    ws = websocket.WebSocket()
    ws.connect('wss://mywesockect')

    while True:        
        data = conn.recv(1024).decode()        
        if not data:
            # if data is not received break
            ws.close()
            break

        print(str(data))
        ws.send(json.dumps( str(data) )) 
    conn.close()  # close the connection

if __name__ == '__main__':
    server_program()
Reply
#3
Hi @josephm,

Thanks for your question! It looks like you're trying to figure out how to use the Python requests library to make a POST request.

The requests library is a great tool for making HTTP requests in Python. To make a POST request, you'll need to use the requests.post() method. This method takes two arguments: the URL of the request, and a data dictionary containing the data you want to send.

For example, if you wanted to make a POST request to the URL "https://example.com/post", you could do something like this:

data = {
    "name": "John Doe",
    "age": 42
}

response = requests.post("https://example.com/post", data=data)
This will make a POST request to the URL with the data you provided. You can then use the response object to access the response data.

I hope this helps! Let me know if you have any other questions.
Reply
#4
Hi there,

It sounds like you're having trouble understanding how to use the Python requests library. Fortunately, there are plenty of resources available to help you out.

The official documentation for the requests library is a great place to start. It provides detailed information about how to use the library, as well as examples of code.

You can also find plenty of tutorials and guides online that can help you get started with the requests library. A quick search on Google should bring up plenty of helpful resources.

Finally, if you have any specific questions about the requests library, you can always ask them here on the Python Forum. There are plenty of experienced Python users who can help you out.

Good luck!
Reply


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 2,761 May-29-2023, 07:50 PM
Last Post: billykid999
  Asyncio | Websockets - general problem starting the server dreamer 5 3,240 Oct-26-2022, 11:55 AM
Last Post: dreamer
  websockets help :/ kimblee 3 5,523 Feb-15-2021, 01:26 AM
Last Post: kimblee
  Slow websockets server -> client pattagghiu 0 2,386 Sep-17-2019, 09:28 AM
Last Post: pattagghiu
  Flask and Websockets == True? rxndy 2 2,808 Apr-21-2019, 04:08 PM
Last Post: rxndy
  Send Pressure sensor data to IoT platform using WebSockets in Python barry76 3 4,616 Mar-12-2019, 09:48 AM
Last Post: barry76
  How to send/receive data over TCP Rehan11 1 3,702 Feb-01-2019, 11:21 AM
Last Post: cetpaseo336
  Send data BMP180 between client and server trought module socket smalhao 0 2,832 Jul-30-2018, 12:56 PM
Last Post: smalhao
  UDP Listen without Bind() GaryFunk 3 8,469 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