LinMinquan's Blog

Experience technology to change life

install PyCrypto on Windows

Environment: Python 2.7 My default python environment is python 3.4, and I create a Virtualenv of python 2.7, when I try to install PyCrypto, it return some error. warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath. building 'Crypto.Random.OSRNG.winrandom' extension error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows SDK 7.1": www.microsoft.com/download/details.aspx?id=8279 I google and get a answer, I need to install prebuild binaries of PyCrypto, below is the page with those binaries for windows Read more →

Invalid HTTP_HOST header Django

环境:Centos, Django 1.11.4, 在运行Django Demo时的错误,运行是以监听所有IP的方式 python manage.py runserver 0.0.0.0:8000 报的错是 Invalid HTTP_HOST header Django. You may need to add u’xxx.xxx.com’ to ALLOWED_HOSTS. 解决办法是在Setting.py中,将u’xxx.xxx.com’(你的域名)加入到ALLOWED_HOSTS中,也把本地的ip也加进去。查了下官方文档,说是为了防止HTTP Host header attacks。 ALLOW_HOST = [u'xxx.xxx.com', 'localhost', '127.0.0.1']   Read more →

Centos 6 Supervisor 开机启动

环境:阿里云Centos 6,Python ,Flask,Gunicorn,Supervisor,Nginx 参考资料: 链接1:centos下通过gunicorn+nginx+supervisor部署Flask项目 链接2:进程管理工具supervisor的使用注意事项 链接3:Setting Up Python and Supervisor on CentOS 链接4:supervisor+gunicorn部署python web项目 链接5:How to control your deamon with Supervisord (On CentOS) 1、supervisor是否要在虚拟环境中安装? 我想了想觉得应该是得在系统中安装。 2、supervisor的配置文件放在哪呢? 原本是放在每个项目的虚拟环境目录下,最佳实践(参考链接3,链接4)是在/etc/目录下建个默认的配置文件 echo_supervisord_conf > supervisord.conf 然后编辑这个配置文件,在最后让这个配置文件include指定目录下的配置文件 [include] files = /etc/supervisord.d/*.conf supervisord.d这个目录也是得自己新建,mkdir /etc/supervisord.d/ 然后再在/etc/supervisord.d/目录里放置所有的supervisor的配置文件。 3、怎样让Centos开机时就让supervisor启动这些配置文件呢? vi /etc/rc.d/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. Read more →

Python Flask Gunicorn Nginx CentOS Deploy SSL Https 多个App 部署

Centos 6,Python 2.7,阿里云ECS, 参考文章: 链接1:centos下通过gunicorn+nginx+supervisor部署Flask项目 链接2:Flask+Gunicorn+Nginx配置多个app 链接3:Virtual Hosts on nginx (CSC309) 我的需求是想在阿里云(Centos)上部署Flask的App,当然我想部署多个App在上面,另外我还会部署https。 链接1的文章写得特别好,但我需要部署https,这个是需要要nginx里配置的。我之前的部署方案也可以支持https,但据说会不稳定,方法是 Python Flask 支持 https Let’s Encrypt 。 nginx的坑。刚开始用nginx时,会发现它有很多个配置文件,网上不同的教程打开的配置文件都不一样,不同操作系统的配置文件目录也可能不同。真是非常崩溃。但后面也很好理解,它其实有一个主的配置文件,主的配置文件里可以包含其它的配置文件,这样它就把配置不同功能的配置文件分开了,比如说有专门配置ssl的配置文件ssl.conf。这样可以减少因为配置错误导致整体功能不可用的情况,单个配置文件错误的话至少只有相应的功能不可用。 其实你启动nginx后,在浏览器中进入nginx的默认页面,它就告诉你了它的配置文件在哪,Centos中是在 /etc/nginx/nginx.conf ,打开这个配置文件,你会注意到它里面有 include 这个词,这个是用于去包含其它目录下的配置文件。 配置https的证书我是在 sslforfree.com 上搞的,用的是Let’s Encrypt 的免费证书。 vi /etc/nginx/conf.d/ssl.conf # # HTTPS server configuration # server { listen 443 ssl http2 default_server; # listen [::]:443 ssl; server_name aliyun.cgtiger.com; root /usr/share/nginx/html; access_log /etc/nginx/conf.d/nginx_access.log; ssl on; ssl_certificate certificate.crt; ssl_certificate_key private.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. Read more →

terminal vi tab 4 spaces 空格

在命令行下,用vi来写代码时,按tab是8个空格,我不确定是否所有vi默认都是8个空格。我是在Mac terminal下发现有这个问题的。解决办法google “vi tab 4 spaces”一下就出来了。按ESC后输入 :set tabstop=4 后回车就可以了。 Read more →

Python Flask 支持 https Let’s Encrypt

有个小的用Flask写的Python程序需要支持https,证书用免费的Let’s Encrypt,在sslforfree.com上输入域名,获得证书。验证域名我用的是在vps上简单搭个ngnix来上传文件验证。 web server用的是Flask的werkzeug内置的server,配置ssl参考下面的链接 https://stackoverflow.com/questions/28579142/attributeerror-context-object-has-no-attribute-wrap-socket/28590266#28590266 http://werkzeug.pocoo.org/docs/0.12/serving/ 只需要多加两行代码即可 if __name__ == "__main__": context = ('cert.crt', 'key.key') app.run(host='0.0.0.0', port=80, ssl_context=context, threaded=True, debug=True) sslforfree中申请后下载得到的压缩包中包含certificate.crt,private.key,ca_bundle.crt。 怎么判断是否我们的Python程序的证书是否有效呢? 可以用chrome 60版本来判断。下面链接教怎么在cheome 60中查看证书。 https://www.ghacks.net/2017/07/27/configure-google-chrome-to-display-certificates-directly/ 这样就可以在chrome中访问某个get接口, 然后就可以查看证书的信息了。   Read more →

SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.

Environment:Mac OS 10.12.2, Python 2.7.10, SQLAlchemy 1.1.4 在试教程 http://www.jikexueyuan.com/course/1387_2.html?ss=1 时,用到python flask_sqlalchemy (version 1.1.4)时,报一个warning /Users/xxx/Develop/flasktest-database/venv/lib/python2.7/site-packages/ flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning. warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.') 解决办法是加一行配置,将SQLALCHEMY_TRACK_MODIFICATIONS设为true,如下 app.config['SQLALCHEMY_DATABASE_URI']="mysql://root:password@localhost:3306/jikexueyuan" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True 参考链接:http://www.jianshu.com/p/70c892fab7af Read more →

command ‘cc’ failed with exit status 69

Environment:Mac OS 10.12.2, Python 2.7.10 On terminal and use pip to install mysql-python (virtualenv is activate), but it get a error command ‘cc’ failed with exit status 69 reference link: http://stackoverflow.com/questions/19769648/cant-install-discount-with-pip-error-command-cc-failed-with-exit-status-1 I need to use xcode-select to install Xcode Command Line Tools Read more →

python SNIMissingWarning InsecurePlatformWarning

环境:CentOS 6.8, Python 2.6.6 敲一些命令时,比如 pip list时,会提示: /usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning. SNIMissingWarning /usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. Read more →

error: command ‘gcc’ failed with exit status 1

环境:CentOS 6.8, python 2.6.6, virtualenv 15.1.0 我是在virtualenv里,想要在里面安装mysql-python,但老报错 error: command 'gcc' failed with exit status 1 参考文章:http://blog.csdn.net/fenglifeng1987/article/details/38057193 退出virtualenv deactivate 然后输入以下命令 yum install gcc libffi-devel python-devel openssl-devel 不过这三个东西分别是什么用途呢? Read more →