![]() |
Listen TCP and send data to websockets - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Listen TCP and send data to websockets (/thread-38798.html) |
Listen TCP and send data to websockets - neoboby - Nov-25-2022 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 ! ![]() hi @snippsat i'm sorry i put my message in the wrong section, coul'd you move it in the Network forum ? thank you ! RE: Listen TCP and send data to websockets - neoboby - Dec-12-2022 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() RE: Listen TCP and send data to websockets - Vadanane - Feb-13-2023 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. RE: Listen TCP and send data to websockets - Vadanane - Feb-13-2023 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! |