Shut down Flask SocketIO Server
<p>For circumstances outside of my control, I need to use the Flask server to serve basic html files, the Flask SocketIO wrapper to provide a web socket interface between any clients and the server. The <code>async_mode</code> has to be <code>threading</code> instead of <code>gevent</code> or <code>eventlet</code>, I understand that it is less efficient to use threading, but I can't use the other two frameworks.</p>

<p>In my unit tests, I need to shut down and restart the web socket server. When I attempt to shut down the server, I get the <code>RunTimeError</code> 'Cannot stop unknown web server.' This is because the function <code>werkzeug.server.shutdown</code> is not found in the Flask Request Environment <code>flask.request.environ</code> object.</p>

<p>Here is how the server is started.</p>

<pre><code>SERVER = flask.Flask(__name__)

WEBSOCKET = flask_socketio.SocketIO(SERVER, async_mode='threading')

WEBSOCKET.run(SERVER, host='127.0.0.1', port=7777)
</code></pre>

<p>Here is the short version of how I'm attempting to shut down the server.</p>

<pre><code>client = WEBSOCKET.test_client(SERVER)

@WEBSOCKET.on('kill')
def killed():
    WEBSOCKET.stop()

try:
    client.emit('kill')
except:
    pass
</code></pre>

<p>The stop method must be called from within a flask request context, hence the weird kill event callback. Inside the stop method, the <code>flask.request.environ</code> has the value</p>

<pre><code>'CONTENT_LENGTH' (40503696) = {str} '0'
'CONTENT_TYPE' (60436576) = {str} ''
'HTTP_HOST' (61595248) = {str} 'localhost'
'PATH_INFO' (60437104) = {str} '/socket.io'
'QUERY_STRING' (60327808) = {str} ''
'REQUEST_METHOD' (40503648) = {str} 'GET'
'SCRIPT_NAME' (60437296) = {str} ''
'SERVER_NAME' (61595296) = {str} 'localhost'
'SERVER_PORT' (61595392) = {str} '80'
'SERVER_PROTOCOL' (65284592) = {str} 'HTTP/1.1'
'flask.app' (65336784) = {Flask} &lt;Flask 'server'&gt;
'werkzeug.request' (60361056) = {Request} &lt;Request 'http://localhost/socket.io' [GET]&gt;
'wsgi.errors' (65338896) = {file} &lt;open file '&lt;stderr&gt;', mode 'w' at 0x0000000001C92150&gt;
'wsgi.input' (65338848) = {StringO} &lt;cStringIO.StringO object at 0x00000000039902D0&gt;
'wsgi.multiprocess' (65369288) = {bool} False
'wsgi.multithread' (65369232) = {bool} False
'wsgi.run_once' (65338944) = {bool} False
'wsgi.url_scheme' (65338800) = {str} 'http'
'wsgi.version' (65338752) = {tuple} &lt;type 'tuple'&gt;: (1, 0)
</code></pre>

<p>My question is, how do I set up the Flask server to have the  <code>werkzeug.server.shutdown</code>method available inside the flask request contexts?</p>

<p>Also this is using Python 2.7</p>

<p>I have good news for you, the testing environment does not use a real server, in that context the client and the server run inside the same process, so the communication between them does not go through the network as it does when you run things for real. Really in this situation there is no server, so there's nothing to stop.</p>

<p>It seems you are starting a real server, though. For unit tests, that server is not used, all you need are your unit tests which import the application and then use a test client to issue socket.io events. I think all you need to do is just not start the server, the unit tests should run just fine without it if all you use is the test client as you show above.</p>

python python-2.7 flask werkzeug flask-socketio