python-socketio connecting to server, but is blocked (not executing anything)

121 Views Asked by At

I am making a sort of an IT automation system, that will let the admin, manage, say around 40 computers. I am using React for Frontend, NodeJS as server (acting as an intermediatory), and a Python agent, that executes different functions based on the Admin Requests. The agent will be running on each of the PCs.

So, the admin PC is running SocketIO Client. This client connects to my server, which is using SocketIO Server. Then, I have the python app, running separately, which also establishes connection with my NodeJS Server. Agent is using python-socketio[client] package.

I have separated the admin, and agent PC by namespaces.

  • /agent
  • /admin

I don't have any problem connecting the Admin PC, and the server. Everything handles well. The problem I am having is with python-socketio.

python app establishes connection to my server. The server also generates a socket id. But the python file, gets blocked, doesn't even executes the print("I'm connected!") line.

agent.py
    import socketio
    
    sio = socketio.Client()
    
    @sio.event
    def connect():
        print("I'm connected!")
    
    @sio.event
    def connect_error(data):
        print("The connection failed!")
    
    @sio.event
    def disconnect():
        print("I'm disconnected!")
    
    @sio.on('info')
    def get_info():
        print("info request received by agent from server")
    
    def main():
        sio.connect('http://localhost:3000', namespaces=['/agent'])
        sio.wait()
    
    if __name__ == '__main__':
        main()
middle.js
    import express from "express";
    import cors from "cors";
    import bodyParser from "body-parser";
    
    // Socket.io imports
    import { createServer } from "http";
    import { Server } from "socket.io";
    
    const app = express();
    
    app.use(cors());
    
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    
    const server = createServer(app);
    const io = new Server(server, {
        cors: {
            origin: "*",
        },
    });
    
    const agentIo = io.of("/agent");
    const adminIo = io.of("/admin");

    agentIo.on("connection", (socket) => {
        console.log("New Agent Connected =>", socket.id);
    });

    adminIo.on("connection", (socket) => {
        console.log("Admin Connected =>", socket.id);
        socket.on("info", () => {
            console.log("info request received by server from admin");
            agentIo.timeout(10000).emit("info", (err, res) => {
                if (err) {
                    console.log(err);
                } else {
                    console.log(res);
                }
            });
        });
    });
    
    server.listen(3000, () => {
        console.log("Listening on port 3000...");
    });
output for server
Listening on port 3000...
Admin Connected => CXgn-KpjvdXoJWA4AAAB
New Agent Connected => 3p9hu1X13Mmx_y25AAAD
info request received by server from admin
info request sent to agent by server
output for agent.py
$ python agent2.py 

as you can see there is no problem in communication between Admin and Server. Only the agent file seems to be not working. The agent file seems to be running, but there is no output to the console.

Should I be running a indefinite loop, instead of sio.wait()? And disconnect programmatically? Because, practically this app should be running as long as the PC is. I am not sure if it is the right way of handling the problem, much less the safest way, but then again, I am not an expert.

Not sure what I am doing wrong, but if anyone can point it out I will be grateful. Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER

im not sure if i understand your problem in right way but i think in your main method you have to pass transports=["websocket"] to your sio.connect() like below:

sio.connect("path", transports=["websocket"], namespaces=["/whatever"]

also you can turn on logger to see what is going on in your code. it helps me a lot before.

sio = socketio.Client(logger=True)