In my project, I created a app
:
the website_chat/views.py
code:
async_mode = 'eventlet'
import os
from django.http import HttpResponse
import socketio
basedir = os.path.dirname(os.path.realpath(__file__))
sio = socketio.Server(async_mode=async_mode)
thread = None
the website_chat/management/commands/runserver.py
:
from django.core.management.commands.runserver import Command as RunCommand
from xxx/website_chat.views import sio
class Command(RunCommand):
help = 'Run the Socket.IO server'
def handle(self, *args, **options):
if sio.async_mode == 'threading':
super(Command, self).handle(*args, **options)
elif sio.async_mode == 'eventlet':
# deploy with eventlet
import eventlet
import eventlet.wsgi
from Qyun.wsgi import application
eventlet.wsgi.server(eventlet.listen(('', 8002)), application)
elif sio.async_mode == 'gevent':
# deploy with gevent
from gevent import pywsgi
from Qyun.wsgi import application
try:
from geventwebsocket.handler import WebSocketHandler
websocket = True
except ImportError:
websocket = False
if websocket:
pywsgi.WSGIServer(
('', 8000), application,
handler_class=WebSocketHandler).serve_forever()
else:
pywsgi.WSGIServer(('', 8000), application).serve_forever()
elif sio.async_mode == 'gevent_uwsgi':
print('Start the application through the uwsgi server. Example:')
print('uwsgi --http :5000 --gevent 1000 --http-websockets '
'--master --wsgi-file django_example/wsgi.py --callable '
'application')
else:
print('Unknown async_mode: ' + sio.async_mode)
In my wsgi.py
:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Qiyun02.settings")
from socketio import Middleware
from website_chat.views import sio
django_app = get_wsgi_application()
application = Middleware(sio, django_app)
But when I runserver I get the bellow error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/middleware.py", line 47, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/socketio/server.py", line 360, in handle_request
return self.eio.handle_request(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/server.py", line 274, in handle_request
environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/socket.py", line 91, in handle_get_request
start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/socket.py", line 133, in _upgrade_websocket
return ws(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/async_eventlet.py", line 15, in __call__
raise RuntimeError('You need to use the eventlet server. '
RuntimeError: You need to use the eventlet server. See the Deployment section of the documentation for more information.
But in the website_chat/views.py
, I have configured the:
async_mode = 'eventlet'
sio = socketio.Server(async_mode=async_mode)
why I still get this error?
the document is there:https://github.com/miguelgrinberg/python-socketio/blob/master/docs/index.rst#eventlet
In the end, I do not follow the example of python-socketio
, I configure the wsgi.py
like bellow:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Qyun.settings")
from socketio import WSGIApp
from website_chat.views import sio
django_app = get_wsgi_application()
application = WSGIApp(sio, django_app)
#
import eventlet
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('', 8000)), application)
It works for this issue now. the port still is 8000
, and the management/commands/runserver.py
also can be delete now.