forked from rspivak/lsbaws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ruslan Spivak
committed
Apr 5, 2015
1 parent
552ef30
commit d6f37e6
Showing
12 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import sys | ||
sys.path.insert(0, './helloworld') | ||
from helloworld import wsgi | ||
|
||
|
||
app = wsgi.application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from flask import Flask | ||
from flask import Response | ||
flask_app = Flask('flaskapp') | ||
|
||
|
||
@flask_app.route('/hello') | ||
def hello_world(): | ||
return Response( | ||
'Hello world from Flask!\n', | ||
mimetype='text/plain' | ||
) | ||
|
||
app = flask_app.wsgi_app |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
Django settings for helloworld project. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/1.7/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/1.7/ref/settings/ | ||
""" | ||
|
||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
import os | ||
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = 'h#!izko_ejn@&0g@a#yu$tpuh-a=v$vvcfq5p*y#@2rzfcojn7' | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = True | ||
|
||
TEMPLATE_DEBUG = True | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = ( | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
) | ||
|
||
MIDDLEWARE_CLASSES = ( | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.auth.middleware.SessionAuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
) | ||
|
||
ROOT_URLCONF = 'helloworld.urls' | ||
|
||
WSGI_APPLICATION = 'helloworld.wsgi.application' | ||
|
||
|
||
# Database | ||
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | ||
} | ||
} | ||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/1.7/topics/i18n/ | ||
|
||
LANGUAGE_CODE = 'en-us' | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
# https://docs.djangoproject.com/en/1.7/howto/static-files/ | ||
|
||
STATIC_URL = '/static/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.conf.urls import patterns, include, url | ||
from django.contrib import admin | ||
|
||
urlpatterns = patterns( | ||
'', | ||
# Examples: | ||
# url(r'^$', 'helloworld.views.home', name='home'), | ||
# url(r'^blog/', include('blog.urls')), | ||
|
||
url(r'^admin/', include(admin.site.urls)), | ||
|
||
(r'^hello', 'helloworld.views.index'), | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.http import HttpResponse | ||
|
||
|
||
def index(request): | ||
return HttpResponse( | ||
'Hello world from Django!\n', | ||
content_type='text/plain' | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
WSGI config for helloworld project. | ||
It exposes the WSGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/ | ||
""" | ||
|
||
import os | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "helloworld.settings") | ||
|
||
from django.core.wsgi import get_wsgi_application | ||
application = get_wsgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "helloworld.settings") | ||
|
||
from django.core.management import execute_from_command_line | ||
|
||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pyramid.config import Configurator | ||
from pyramid.response import Response | ||
|
||
|
||
def hello_world(request): | ||
return Response( | ||
'Hello world from Pyramid!\n', | ||
content_type='text/plain', | ||
) | ||
|
||
config = Configurator() | ||
config.add_route('hello', '/hello') | ||
config.add_view(hello_world, route_name='hello') | ||
app = config.make_wsgi_app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# Tested with Python 2.7.9, Linux & Mac OS X | ||
import socket | ||
import StringIO | ||
import sys | ||
|
||
|
||
class WSGIServer(object): | ||
|
||
address_family = socket.AF_INET | ||
socket_type = socket.SOCK_STREAM | ||
request_queue_size = 1 | ||
|
||
def __init__(self, server_address): | ||
# Create a listening socket | ||
self.listen_socket = listen_socket = socket.socket( | ||
self.address_family, | ||
self.socket_type | ||
) | ||
# Allow to reuse the same address | ||
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
# Bind | ||
listen_socket.bind(server_address) | ||
# Activate | ||
listen_socket.listen(self.request_queue_size) | ||
# Get server host name and port | ||
host, port = self.listen_socket.getsockname()[:2] | ||
self.server_name = socket.getfqdn(host) | ||
self.server_port = port | ||
# Return headers set by Web framework/Web application | ||
self.headers_set = [] | ||
|
||
def set_app(self, application): | ||
self.application = application | ||
|
||
def serve_forever(self): | ||
listen_socket = self.listen_socket | ||
while True: | ||
# New client connection | ||
self.client_connection, client_address = listen_socket.accept() | ||
# Handle one request and close the client connection. Then | ||
# loop over to wait for another client connection | ||
self.handle_one_request() | ||
|
||
def handle_one_request(self): | ||
self.request_data = request_data = self.client_connection.recv(1024) | ||
# Print formatted request data a la 'curl -v' | ||
print(''.join( | ||
'< {line}\n'.format(line=line) | ||
for line in request_data.splitlines() | ||
)) | ||
|
||
self.parse_request(request_data) | ||
|
||
# Construct environment dictionary using request data | ||
env = self.get_environ() | ||
|
||
# It's time to call our application callable and get | ||
# back a result that will become HTTP response body | ||
result = self.application(env, self.start_response) | ||
|
||
# Construct a response and send it back to the client | ||
self.finish_response(result) | ||
|
||
def parse_request(self, text): | ||
request_line = text.splitlines()[0] | ||
request_line = request_line.rstrip('\r\n') | ||
# Break down the request line into components | ||
(self.request_method, # GET | ||
self.path, # /hello | ||
self.request_version # HTTP/1.1 | ||
) = request_line.split() | ||
|
||
def get_environ(self): | ||
env = {} | ||
# The following code snippet does not follow PEP8 conventions | ||
# but it's formatted the way it is for demonstration purposes | ||
# to emphasize the required variables and their values | ||
# | ||
# Required WSGI variables | ||
env['wsgi.version'] = (1, 0) | ||
env['wsgi.url_scheme'] = 'http' | ||
env['wsgi.input'] = StringIO.StringIO(self.request_data) | ||
env['wsgi.errors'] = sys.stderr | ||
env['wsgi.multithread'] = False | ||
env['wsgi.multiprocess'] = False | ||
env['wsgi.run_once'] = False | ||
# Required CGI variables | ||
env['REQUEST_METHOD'] = self.request_method # GET | ||
env['PATH_INFO'] = self.path # /hello | ||
env['SERVER_NAME'] = self.server_name # localhost | ||
env['SERVER_PORT'] = str(self.server_port) # 8888 | ||
return env | ||
|
||
def start_response(self, status, response_headers, exc_info=None): | ||
# Add necessary server headers | ||
server_headers = [ | ||
('Date', 'Tue, 31 Mar 2015 12:54:48 GMT'), | ||
('Server', 'WSGIServer 0.2'), | ||
] | ||
self.headers_set = [status, response_headers + server_headers] | ||
# To adhere to WSGI specification the start_response must return | ||
# a 'write' callable. We simplicity's sake we'll ignore that detail | ||
# for now. | ||
# return self.finish_response | ||
|
||
def finish_response(self, result): | ||
try: | ||
status, response_headers = self.headers_set | ||
response = 'HTTP/1.1 {status}\r\n'.format(status=status) | ||
for header in response_headers: | ||
response += '{0}: {1}\r\n'.format(*header) | ||
response += '\r\n' | ||
for data in result: | ||
response += data | ||
# Print formatted response data a la 'curl -v' | ||
print(''.join( | ||
'> {line}\n'.format(line=line) | ||
for line in response.splitlines() | ||
)) | ||
self.client_connection.sendall(response) | ||
finally: | ||
self.client_connection.close() | ||
|
||
|
||
SERVER_ADDRESS = (HOST, PORT) = '', 8888 | ||
|
||
|
||
def make_server(server_address, application): | ||
server = WSGIServer(server_address) | ||
server.set_app(application) | ||
return server | ||
|
||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) < 2: | ||
sys.exit('Provide a WSGI application object as module:callable') | ||
app_path = sys.argv[1] | ||
module, application = app_path.split(':') | ||
module = __import__(module) | ||
application = getattr(module, application) | ||
httpd = make_server(SERVER_ADDRESS, application) | ||
print('WSGIServer: Serving HTTP on port {port} ...\n'.format(port=PORT)) | ||
httpd.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
def app(environ, start_response): | ||
"""A barebones WSGI application. | ||
This is a starting point for your own Web framework :) | ||
""" | ||
status = '200 OK' | ||
response_headers = [('Content-Type', 'text/plain')] | ||
start_response(status, response_headers) | ||
return ['Hello world from a simple WSGI application!\n'] |