Skip to content

Instantly share code, notes, and snippets.

@laiwei
Created April 8, 2013 07:50
Show Gist options
  • Save laiwei/5335001 to your computer and use it in GitHub Desktop.
Save laiwei/5335001 to your computer and use it in GitHub Desktop.
python web dev

python web 开发

先学习python

div into python -- 中文版本

div into python 这是一本python入门的小书,非常经典,不用花太久就能读完,然后就能开始着手写代码了!

准备python开发环境

virtualenv 来维护自己的python开发环境

pip 用来安装、管理python的第三方包

virtualenv

安装

sudo apt-get install python-virtualenv

用virtualenv生成一个隔离的python环境

mkdir -p /tmp/myproj/ && cd /tmp/myproj/ && virtualenv env

这样就在 /tmp/myproj/ 目录下面,生成了一个env的目录,其中包括了一份python环境的完整copy

激活并使用这个隔离的环境

source /tmp/myproj/env/bin/activate

这时,我们就处于这个独立的隔离环境中了。

装个第三方python lib来用用, 我们就装flask吧,这是一个python的web开发环境,很轻量的框架

pip install flask

ok,写个hello world的web应用吧!

hello.py

#-*- coding:utf-8 -*-

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

运行看看结果:

$ python hello.py
* Running on http://localhost:5000/

req.py

#-*- coding:utf-8 -*-

from flask import Flask, request
app = Flask(__name__)

@app.route("/user")
def myuser():
    name = request.args.get("name", "xx") 
    return "Hello %s!" % name

if __name__ == "__main__":
    app.run()

运行看看结果:

$ python req.py
* Running on http://localhost:5000/

进一步学习flask (主要学习 template

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.

Flask quickstart -- flask 在线文档,很齐全,这是一个quickstart,推荐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment