title: Python to Python WebSockets with websockets and flask-socketio
body: <h1>Problem</h1>
  
  <p>So I need to make a python application that can sit on a machine, open up a websocket pipe to a server running flask-socketio, then pipe data across to the server. The data will be json data, for now I am testing with a string. </p>
  
  <p>I am having an issue using the 'websockets' library in python to connect to the 'flask-socketio' backend that I am making.</p>
  
  <h1>Code</h1>
  
  <h2>Server</h2>
  
  <pre><code>from flask import Flask, render_template, request, url_for, 
  
  copy_current_request_context
  from flask_socketio import SocketIO, emit
  import logging
  
  logging.basicConfig()
  
  app = Flask(__name__)
  app.config['SECRET_KEY'] = 'secret!'
  app.config['DEBUG'] = True
  socketio = SocketIO(app)
  
  
  @socketio.on('connect', namespace='/')
  def connect():
      print('Client connected')
  
  
  @socketio.on('disconnect', namespace='/')
  def disconnect():
      print('Client disconnected')
  
  
  if __name__ == '__main__':
      socketio.run(app, debug=True)
  </code></pre>
  
  <h2>Client 1 - websockets library</h2>
  
  <pre><code>import asyncio
  import websockets
  
  async def hello():
      async with websockets.connect(
              'ws://127.0.0.1:5000') as websocket:
          name = 'joe'  # input("What's your name? ")
  
          await websocket.send('message', name)
          print(f"&gt; {name}")
  
          greeting = await websocket.recv()
          print(f"&lt; {greeting}")
  
  asyncio.get_event_loop().run_until_complete(hello())
  
  print("end")
  </code></pre>
  
  <h3>Use output</h3>
  
  <p>When I use client 1 I get this as output on the client process:
  <code>Traceback (most recent call last):
    File "C:/project_files/aWebUAS/micro-services/New-Capabilities-Investigation/web_socket_client.py", line 15, in &lt;module&gt;
      asyncio.get_event_loop().run_until_complete(hello())
    File "C:\Program Files\Anaconda3\lib\asyncio\base_events.py", line 466, in run_until_complete
      return future.result()
    File "C:/project_files/aWebUAS/micro-services/New-Capabilities-Investigation/web_socket_client.py", line 6, in hello
      'ws://127.0.0.1:5000') as websocket:
    File "C:\Program Files\Anaconda3\lib\site-packages\websockets\py35\client.py", line 2, in __aenter__
      return await self
    File "C:\Program Files\Anaconda3\lib\site-packages\websockets\py35\client.py", line 20, in __await_impl__
      extra_headers=protocol.extra_headers,
    File "C:\Program Files\Anaconda3\lib\site-packages\websockets\client.py", line 286, in handshake
      raise InvalidStatusCode(status_code)
  websockets.exceptions.InvalidStatusCode: Status code not 101: 404</code></p>
  
  <p>and on the flask process I see:<br>
  <code>127.0.0.1 - - [2019-04-05 09:17:13] "GET / HTTP/1.1" 404 342 0.000999</code> </p>
  
  <h2>Client 2 - websocket-client library</h2>
  
  <pre><code>import websocket
  ws = websocket.WebSocket()
  ws.connect("ws://127.0.0.1:5000")
  ws.send("Hello, World")
  </code></pre>
  
  <h3>Use output</h3>
  
  <p>On the client 2 process I get this as output:
  <code>Traceback (most recent call last):
    File "C:/project_files/aWebUAS/micro-services/New-Capabilities-Investigation/web_socket_client_2.py", line 3, in &lt;module&gt;
      ws.connect("ws://127.0.0.1:5000")
    File "C:\Program Files\Anaconda3\lib\site-packages\websocket\_core.py", line 226, in connect
      self.handshake_response = handshake(self.sock, *addrs, **options)
    File "C:\Program Files\Anaconda3\lib\site-packages\websocket\_handshake.py", line 79, in handshake
      status, resp = _get_resp_headers(sock)
    File "C:\Program Files\Anaconda3\lib\site-packages\websocket\_handshake.py", line 160, in _get_resp_headers
      raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
  websocket._exceptions.WebSocketBadStatusException: Handshake status 404 NOT FOUND</code></p>
  
  <p>and the flask process shows this:<br>
  <code>127.0.0.1 - - [2019-04-05 09:11:57] "GET / HTTP/1.1" 404 342 0.001000</code></p>
  
  <h1>Conclusion</h1>
  
  <p>Some reading around tells me I am not hitting my socketio handler within flask properly, but I am unsure how to target that.</p>
  
  <p>Any help is much appreciated. My goal is to have a python script talking to a flask socketio server. I am not married to any part of this but would prefer to stick to flask for the server. </p>
  
  <p>TIA - Ian</p>