Skip to content

Instantly share code, notes, and snippets.

View djoreilly's full-sized avatar

Darragh O'Reilly djoreilly

View GitHub Profile
@djoreilly
djoreilly / paste.ini
Created March 20, 2013 21:59
simple pastedeploy config
[pipeline:main]
pipeline = auth hello
[app:hello]
paste.app_factory = app_layer:app_factory
[filter:auth]
paste.filter_factory = auth_layer:filter_factory
@djoreilly
djoreilly / app_layer.py
Last active December 15, 2015 05:29
application layer
from webob import Response
from webob.dec import wsgify
@wsgify
def application(request):
return Response('Hello, welcome to paste \n')
def app_factory(global_config, **local_config):
return application
@djoreilly
djoreilly / auth_layer.py
Created March 20, 2013 22:04
webob middleware layer
from webob.dec import wsgify
from webob import exc
@wsgify.middleware
def auth_filter(request, app):
if request.headers.get('X-Auth-Token') != 'open-sesame':
return exc.HTTPForbidden()
return app(request)
def filter_factory(global_config, **local_config):
from paste import httpserver
from paste.deploy import loadapp
wsgi_app = loadapp('config:' + '/etc/example/paste.ini')
httpserver.serve(wsgi_app, host='127.0.0.1', port=8080)
import sys
import urllib2
req = urllib2.Request('http://127.0.0.1:8080')
req.add_header('X-Auth-Token', sys.argv[1])
try:
res = urllib2.urlopen(req)
print res.read()
except urllib2.HTTPError as e:
$ python start_app.py
serving on http://127.0.0.1:8080
$ python do_get.py wrong-token
HTTP Error 403: Forbidden
$ py do_get.py open-sesame
Hello, welcome to paste
@djoreilly
djoreilly / paste-console
Created March 21, 2013 11:29
console output
$ python start_app.py
serving on http://127.0.0.1:8080
$ curl -H "X-Auth-Token:open-sesame" http://127.0.0.1:8080
Hello, welccome to paste
$ curl -H "X-Auth-Token:bad-token" http://127.0.0.1:8080
<html>
@djoreilly
djoreilly / spawn.py
Last active December 15, 2015 05:59
import eventlet
def add(x, y):
return x + y
e = eventlet.spawn(add, 1, 2)
print e
res = e.wait()
print '1 + 2 =', res
@djoreilly
djoreilly / co-op.py
Last active December 15, 2015 05:59
$ python co-op.py <br /> func2 <br /> func1
import eventlet
def func1():
# suspend me and run something else
# but switch back to me after 2 seconds (if you can)
eventlet.sleep(2)
print "func1"
def func2():
eventlet.sleep(1)
import eventlet
from eventlet import event
evt = event.Event()
def waiter():
evt.wait()
print "waiter"
w = eventlet.spawn(waiter)