M
M
mr_br2017-03-29 16:09:31
Python
mr_br, 2017-03-29 16:09:31

How to issue a log on a remote computer through sockets?

Faced the following task: There is a server to which clients are connected through a websocket. One client is a remote computer that connects to the server when the HTML file is opened. An HTML file that, using JS, connects to the server via a socket and simply displays each message received from it. The code:

<!DOCTYPE html>
<html>
    <head>
        <title>SGW UI</title>
    </head>
    <body>
        <script>
            var ws = new WebSocket("ws://130.83.40.174:5678/"),
                messages = document.createElement('ul');
            ws.onmessage = function (event) {
                var messages = document.getElementsByTagName('ul')[0],
                    message = document.createElement('li'),
                    content = document.createTextNode(event.data);
                message.appendChild(content);
                messages.appendChild(message);
            };
            document.body.appendChild(messages);
        </script>
    </body>
</html>


The rest of the clients are bash scripts that also connect to the server, send some message and disconnect. The server actually should transfer these messages to the first browser client. I have problems with writing a server. I found a simple socket library. The first idea was for the browser client to connect first, its socket object to be saved, and on subsequent client connections (bash scripts), the message would simply be sent to the saved socket.

Server code:
#!/usr/bin/python3.4
import asyncio
#!/usr/bin/python3.4
import datetime
import random
import websockets
import sys
browser_client = None
@asyncio.coroutine
def socket_handler(websocket, path):
    global browser_client
    if(browser_client is None):
        browser_client = websocket
    else:
        m = yield from websocket.recv()
        browser_client.send(m)
start_server = websockets.serve(socket_handler, '130.83.40.174', 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


As a result, this solution did not work, I read on stackoverflow that socket objects cannot be saved. Then this idea came to mind:

browser_client = None
messages = []
@asyncio.coroutine
def socket_handler(websocket, path):
    global browser_client
    if(browser_client is None):
        while True:
            if(len(messages) > 0):
                msg = messages.pop()
                yield from websocket.send(msg)
    else:
        m = yield from websocket.recv()
        messages.append(m)


But it didn’t reach the service of other clients except the browser one and the server was constantly in the while loop of the first client, apparently I don’t fully understand the essence of @asyncio.coroutine.

Well, the code with which I imitated other (bash) clients:

#!/usr/bin/env python
import asyncio
import websockets
@asyncio.coroutine
def hello():
    websocket = yield from websockets.connect(u'ws://130.83.40.174:5678')
    name = "Test"
    yield from websocket.send(name)
    yield from websocket.close()
asyncio.get_event_loop().run_until_complete(hello())


In the end, I hit a dead end. I would be grateful if someone could tell me how to solve this problem with sockets

) language version python3.4 is used

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asd111, 2017-03-29
@asd111

Try https://flask-socketio.readthedocs.io/en/latest/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question