Skip to content

Instantly share code, notes, and snippets.

View longfin's full-sized avatar

Swen Mun longfin

View GitHub Profile
@longfin
longfin / gist:1616212
Created January 15, 2012 15:43
Simple Flask Application
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
@longfin
longfin / gist:1616249
Created January 15, 2012 15:54
Flask Routes
# http://flask.pocoo.org/docs/api/#url-route-registrations
@app.route('/')
def index():
pass
@app.route('/<username>')
def show_user(username):
pass
@longfin
longfin / gist:1616290
Created January 15, 2012 16:11
Flask url_for
@app.route('/')
def index():
return ""
@app.route('/<username>')
def show_user(username):
return username
@app.route('/post/<int:post_id>')
def show_post(post_id):
@longfin
longfin / gist:1616333
Created January 15, 2012 16:26
flask reverse
from flask import request
@app.route("/reverse")
def reverse():
message = request.values["message"]
return "".join(reversed(message))
@longfin
longfin / gist:1616353
Created January 15, 2012 16:33
Flask session
# from http://flask.pocoo.org/docs/quickstart/#sessions
from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
@app.route('/')
def index():
if 'username' in session:
return 'Logged in as %s' % escape(session['username'])
@longfin
longfin / gist:1616379
Created January 15, 2012 16:44
Flask rendering
# from http://flask.pocoo.org/docs/quickstart/#rendering-templates
from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
@longfin
longfin / gist:1808715
Created February 12, 2012 14:08
asyncore
# http://docs.python.org/library/asyncore.html#asyncore-example-basic-http-client
import asyncore, socket
class HTTPClient(asyncore.dispatcher):
def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect( (host, 80) )
self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
@longfin
longfin / gist:1808721
Created February 12, 2012 14:09
before eventlet...
# 페이스북 계정들을 가져와서 반복하면서
for account in FacebookAccount.query:
account.update() #FQL을 보내자.
@longfin
longfin / gist:1808724
Created February 12, 2012 14:10
after eventlet
import eventlet
eventlet.monkey_patch() #표준 라이브러리를 변환
# 여러가지 import를 하고...
pool = eventlet.GreenPool()
# 페이스북 계정들을 가져와서 반복하면서
for account in FacebookAccount.query:
# 코루틴들에게 떠넘기자.
@longfin
longfin / gist:1812372
Created February 13, 2012 01:04
eventlet.pathcer.monkey_patch
# from eventlet/pathcer.py
def monkey_patch(**on):
"""Globally patches certain system modules to be greenthread-friendly.
The keyword arguments afford some control over which modules are patched.
If no keyword arguments are supplied, all possible modules are patched.
If keywords are set to True, only the specified modules are patched. E.g.,
``monkey_patch(socket=True, select=True)`` patches only the select and
socket modules. Most arguments patch the single module of the same name