【1】Flask初识

Flask是一个使用 Python 编写的轻量级 Web 应用框架。其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2。

中文官网:http://dormousehole.readthedocs.io/en/latest/

一、Flask的安装

http://flask.pocoo.org/docs/0.10/installation/#installation
pip install --user virtualenv
mkdir myproject
cd myproject
virtualenv venv
. venv/bin/activate

If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:
flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.

二、简单的技巧

1.模板和静态文件 存放在应用的 Python 源代码树的子目录中,名称分别为 templates 和 static

2.引入外部Html

	{% include 'footer.html' %}

因为Flask是基于Jinjia2的模板引擎

{% extends "layout.html" %}
{% block body %}
	< ul >
	{% for user in users %}
		< li >< a href = " {{ user.url }} " >  {{ user.username }} < /a > < /li >
	{% endfor %}
	< /ul>
{% endblock %}

3.调用模板以及参数的引入

render_template

参数的引入:http://www.programcreek.com/python/example/51521/flask.render_template

@app.route('/feedback/view/')
def one_feedback(feedback_id):
		feedback_info = get_one_feedback(feedback_id)
		feedback_info["api_loc"] = api_id
		feedback_info["feedback_id"] = feedback_id

		logger.info("zzz")
		return render_template('one_result.html', feedback=feedback_info)

one_result.html中需加入参数的地方:
{{ feedback["feedback_id"] }}

4.插入参数避免转义

Flask 的模板 Jinja2 会将所有的 html 转义,我想让输出的 、 等指定的标签不进行转义,让他们正常输出

	{{ some html string|safe }}

5.多线程

	app.run(host=host, debug=True, port=port ,<span style="color: #ff0000;">threaded = True</span>)

6.Apache的部署

	vim /etc/httpd/conf/httpd.conf
	# Load config files in the "/etc/httpd/conf.d" directory, if any.
	
	354 IncludeOptional conf.d/*.conf
	355 IncludeOptional /bioinfo/web/httpd/lizard_conf/*.conf

配置好genoprime.conf,放在/bioinfo/web/httpd/lizard_conf/文件夹下

genoprime.conf的配置

	1 Listen 5555
Listen 5555 

<Directory "/bioinfo/web/Tools/GenoPrimeWeb">
				Options Indexes FollowSymLinks
				AllowOverride All
				Require all granted
				Allow from all
				Order allow,deny



		WSGIScriptAlias / /bioinfo/web/Tools/GenoPrimeWeb/flask.wsgi
		ServerName genoprime.genowise.com
		DocumentRoot "/bioinfo/web/Tools/GenoPrimeWeb"
		ServerAdmin wwu@genowise.com
		DirectoryIndex index.php
		ErrorLog "/bioinfo/web/httpd/log/genoprime_error_log"
		CustomLog "/bioinfo/web/httpd/log/genoprime_access_log" common



# More VirtualHosts by port may be added - please see <*:5555>
# Note: When Firewall is on, you may need open the port you use

配置的过程中一直报错,一个是文件夹权限的问题;

7.Flask + Gunicorn + Nginx 部署

http://www.cnblogs.com/Ray-liang/p/4837850.html

vim /bioinfo/web/nginx/lizard_conf/ngs.conf

server {
 listen 7000;
 server_name 192.168.0.11;

 location / {
 proxy_pass http://127.0.0.1:7003;
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_read_timeout 3000s;
 }
}

重启ngix,

cd /home/qqin/ngs/GenoNGS_dev/service
./venv/bin/activate
gunicorn -w 4 -b 127.0.0.1:7003 run:app

后续,可以加到启动启动中。。。

通过7000来端口来访问网站,但通过7003端口来监听genongs服务,这个7003跟genongs中config给的端口没有什么关系;run:app 分别代表run的脚本,以及这个脚本app的实例

gunicorn的安装

pip install gunicorn

http://docs.gunicorn.org/en/stable/install.html

三、报错:

1.socket.error: [Errno 48] Address already in use

解决办法:

$ ps -fA | grep python
	501 81651 12648   0  9:53PM ttys000    0:00.16 python -m SimpleHTTPServer

The command arguments are included, so you can spot the one running SimpleHTTPServer if more than one python process is active. You may want to test if http://localhost:8000/ still shows a directory listing for local files. The second number is the process number; stop the server by sending it a signal:

kill 81651

参考资料:

http://stackoverflow.com/questions/19071512/socket-error-errno-48-address-already-in-use

这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn