Skip to content Skip to sidebar Skip to footer

Error 400 (bad Request) When Connecting Socket From Electron Ui To Python Server

Im having some problem when joining Electron and Python using web sockets (mostly as a learning experience): Build a desktop app using Electron Ok Build a Python program that moni

Solution 1:

In case someone hits the same problem. The answer is pretty simple, from the documentation for socket.io here:

For security reasons, this server enforces a same-origin policy by default. In practical terms, this means the following:

  • If an incoming HTTP or WebSocket request includes the Origin header, this header must match the scheme and host of the connection URL. In case of a mismatch, a 400 status code response is returned and the connection is rejected.
  • No restrictions are imposed on incoming requests that do not include the Origin header.

If necessary, the cors_allowed_origins option can be used to allow other origins. This argument can be set to a string to set a single allowed origin, or to a list to allow multiple origins. A special value of '*' can be used to instruct the server to allow all origins, but this should be done with care, as this could make the server vulnerable to Cross-Site Request Forgery (CSRF) attacks.

When using electron to load the gui, the server is not the same as the python server that's doing the monitoring, thus the Bad Request (not same origin). The solution is to just modify as follows the server properties adding cors_allowed_origins='*':

socketio.AsyncServer(async_mode='aiohttp', cors_allowed_origins='*')

Post a Comment for "Error 400 (bad Request) When Connecting Socket From Electron Ui To Python Server"