Skip to content

Commit 3531b51

Browse files
Django example
1 parent a40e2ab commit 3531b51

File tree

18 files changed

+440
-5
lines changed

18 files changed

+440
-5
lines changed

examples/wsgi/README.rst

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ Socket.IO WSGI Examples
22
=======================
33

44
This directory contains example Socket.IO applications that work together with
5-
WSGI frameworks. These examples all use Flask to serve the client application to
6-
the web browser, but they should be easily adapted to use other WSGI compliant
7-
frameworks.
5+
WSGI frameworks. These examples use Flask or Django to serve the client
6+
application to the web browser, but they should be easily adapted to use other
7+
WSGI compliant frameworks.
88

99
app.py
1010
------
@@ -24,6 +24,12 @@ time to the page.
2424
This is an ideal application to measure the performance of the different
2525
asynchronous modes supported by the Socket.IO server.
2626

27+
django_example
28+
--------------
29+
30+
This is a version of the "app.py" application described above, that is based
31+
on the Django web framework.
32+
2733
Running the Examples
2834
--------------------
2935

@@ -36,13 +42,20 @@ or::
3642

3743
$ python latency.py
3844

45+
or::
46+
47+
$ cd django_example
48+
$ ./manage.py runserver
49+
3950
You can then access the application from your web browser at
40-
``http://localhost:5000``.
51+
``http://localhost:5000`` (``app.py`` and ``latency.py``) or
52+
``http://localhost:8000`` (``django_example``).
4153

4254
Near the top of the ``app.py`` and ``latency.py`` source files there is a
4355
``async_mode`` variable that can be edited to swich to the other asynchornous
4456
modes. Accepted values for ``async_mode`` are ``'threading'``, ``'eventlet'``
45-
and ``'gevent'``.
57+
and ``'gevent'``. For ``django_example``, the async mode can be set in the
58+
``django_example/socketio_app/views.py`` module.
4659

4760
Note 1: when using the ``'eventlet'`` mode, the eventlet package must be
4861
installed in the virtual environment::

examples/wsgi/django_example/django_example/__init__.py

Whitespace-only changes.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
Django settings for django_example project.
3+
4+
Generated by 'django-admin startproject' using Django 1.11.1.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.11/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/1.11/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '+vk#7#92ncb*y)8^$7sd&99%^+xc+t)nmamacbp8^vgjy(&g-9'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'socketio_app',
35+
'django.contrib.admin',
36+
'django.contrib.auth',
37+
'django.contrib.contenttypes',
38+
'django.contrib.sessions',
39+
'django.contrib.messages',
40+
'django.contrib.staticfiles',
41+
]
42+
43+
MIDDLEWARE = [
44+
'django.middleware.security.SecurityMiddleware',
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
]
52+
53+
ROOT_URLCONF = 'django_example.urls'
54+
55+
TEMPLATES = [
56+
{
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [],
59+
'APP_DIRS': True,
60+
'OPTIONS': {
61+
'context_processors': [
62+
'django.template.context_processors.debug',
63+
'django.template.context_processors.request',
64+
'django.contrib.auth.context_processors.auth',
65+
'django.contrib.messages.context_processors.messages',
66+
],
67+
},
68+
},
69+
]
70+
71+
WSGI_APPLICATION = 'django_example.wsgi.application'
72+
73+
74+
# Database
75+
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
76+
77+
DATABASES = {
78+
'default': {
79+
'ENGINE': 'django.db.backends.sqlite3',
80+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
81+
}
82+
}
83+
84+
85+
# Password validation
86+
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
87+
88+
AUTH_PASSWORD_VALIDATORS = [
89+
{
90+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
91+
},
92+
{
93+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
94+
},
95+
{
96+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
97+
},
98+
{
99+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
100+
},
101+
]
102+
103+
104+
# Internationalization
105+
# https://docs.djangoproject.com/en/1.11/topics/i18n/
106+
107+
LANGUAGE_CODE = 'en-us'
108+
109+
TIME_ZONE = 'UTC'
110+
111+
USE_I18N = True
112+
113+
USE_L10N = True
114+
115+
USE_TZ = True
116+
117+
118+
# Static files (CSS, JavaScript, Images)
119+
# https://docs.djangoproject.com/en/1.11/howto/static-files/
120+
121+
STATIC_URL = '/static/'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""django_example URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/1.11/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.conf.urls import url, include
14+
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
15+
"""
16+
from django.conf.urls import url, include
17+
from django.contrib import admin
18+
19+
urlpatterns = [
20+
url(r'', include('socketio_app.urls')),
21+
url(r'^admin/', admin.site.urls),
22+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
WSGI config for django_example project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
from socketio import Middleware
14+
15+
from socketio_app.views import sio
16+
17+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_example.settings")
18+
19+
django_app = get_wsgi_application()
20+
application = Middleware(sio, django_app)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_example.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError:
10+
# The above import may fail for some other reason. Ensure that the
11+
# issue is really that Django is missing to avoid masking other
12+
# exceptions on Python 2.
13+
try:
14+
import django
15+
except ImportError:
16+
raise ImportError(
17+
"Couldn't import Django. Are you sure it's installed and "
18+
"available on your PYTHONPATH environment variable? Did you "
19+
"forget to activate a virtual environment?"
20+
)
21+
raise
22+
execute_from_command_line(sys.argv)

examples/wsgi/django_example/socketio_app/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class SocketioAppConfig(AppConfig):
5+
name = 'socketio_app'

examples/wsgi/django_example/socketio_app/management/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)