title: Error 400 (Bad Request) when connecting socket from electron UI to python server
body: <p>Im having some problem when joining Electron and Python using web sockets (mostly as a learning experience):</p>
  
  <ul>
  <li><p>Build a desktop app using Electron <strong>Ok</strong></p></li>
  <li><p>Build a Python program that monitors <em>some things</em> <strong>Ok</strong></p></li>
  <li><p>Connect Python with Electron using socket.io <strong>Not working</strong></p></li>
  </ul>
  
  <p>First I tested socket.io in python with the 'latency' example in <a href="https://github.com/miguelgrinberg/python-socketio/tree/master/examples/server/aiohttp" rel="nofollow noreferrer">https://github.com/miguelgrinberg/python-socketio/tree/master/examples/server/aiohttp</a>. It worked great when I took a look at <a href="http://localhost:8080/" rel="nofollow noreferrer">http://localhost:8080/</a> in Chrome.</p>
  
  <p>The next step was to ask Electron to load the same url in the desktop app with <code>mainWindow.loadURL('http://localhost:8080')</code> (I used the template from the official <code>electron-quick-start</code> example). I just had to add two lines into the html as shown in here: <a href="https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined">Electron: jQuery is not defined</a> for jquery to load correctly in Electron, but it again worked nicely.</p>
  
  <p>Now I tried to create an <code>index.html</code> with the same content as <code>latency.html</code> and load it with <code>mainWindow.loadFile('gui/index.html')</code>. I changed the line <code>var socket = io.connect();</code> for <code>var socket = io.connect('ws://localhost:8080');</code> (I did also tried <code>ws://127.0.0.1:8080</code> and <code>ws://192.168.&lt;x&gt;.&lt;x&gt;:8080</code>). The html file loads correctly but the socket begins to connect and disconnect repeatedly with a Bad Request error (I added two console.log lines):</p>
  
  <pre><code>(console.log) connected
  (error)       POST http://localhost:8080/socket.io/?EIO=3&amp;transport=polling&amp;t=MosNuF3&amp;sid=c62ce5a6090c4b72bf3f7c6916da6ce7 400 (Bad Request)   **polling-xhr.js:264**
  (console.log) disconnected
  (warning)     websocket.js:235 WebSocket connection to 'ws://localhost:8080/socket.io/?EIO=3&amp;transport=websocket&amp;sid=c62ce5a6090c4b72bf3f7c6916da6ce7' failed: WebSocket is closed before the connection is established.
  (error)       POST http://localhost:8080/socket.io/?EIO=3&amp;transport=polling&amp;t=MosNuFS&amp;sid=c62ce5a6090c4b72bf3f7c6916da6ce7 400 (Bad Request)   **polling-xhr.js:264**
  (console.log) connected
  etc...
  (console.log) disconnected
  etc...
  </code></pre>
  
  <p>On the server side there is also an error when I stop the Electron app: </p>
  
  <pre><code>Unhandled exception
  Traceback (most recent call last):
    File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_protocol.py", line 447, in start
      await resp.prepare(request)
    File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_response.py", line 353, in prepare
      return await self._start(request)
    File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_response.py", line 667, in _start
      return await super()._start(request)
    File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\web_response.py", line 410, in _start
      await writer.write_headers(status_line, headers)
    File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\http_writer.py", line 112, in write_headers
      self._write(buf)
    File "D:\Programming\Tools\Anaconda3\lib\site-packages\aiohttp\http_writer.py", line 67, in _write
      raise ConnectionResetError('Cannot write to closing transport')
  ConnectionResetError: Cannot write to closing transport
  </code></pre>
  
  <p>What does this Bad Request error mean in this context ? How would the connection work correctly ? </p>
  
  <p>Thanks. </p>