centOS 6.5采用python+nginx+uwsgi实现爬金十财经日历

时间:2023-03-09 04:06:11
centOS 6.5采用python+nginx+uwsgi实现爬金十财经日历

上一篇中有关于安装nginx、python、uwsgi的过程,这里不再重述。下面是有关在具体布署中的一些过程和问题处理

一、因为用到了bs4(BeautifulSoup)\paste\lxml所以这些先安装,pip安装即可

二、nginx端口更改为了8001,防止与原来已经存在的apache服务器冲突,uwsgi使用了8010的端口。其中在测试的时候发现返回为200 ok,但没有输出内容,后来网上看到要注意python的版本,我用的是python3。

# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2

三、测试uwsgi的时候用

uwsgi --http :8001 --wsgi-file test.py

但实际使用中要开机启动,所以参考了http://www.cnblogs.com/xiongpq/p/3381069.html,

#! /bin/sh
# chkconfig:
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi' ### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start:
# Default-Stop:
# Short-Description: starts the uwsgi web server
# Description: starts uwsgi using start-stop-daemon
### END INIT INFO # Author: licess
# website: http://lnmp.org PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi8010
DAEMON=/usr/bin/uwsgi //注意你的安装位置
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME set -e
[ -x "$DAEMON" ] || exit do_start() {
$DAEMON $CONFIGFILE || echo -n "uwsgi already running"
} do_stop() {
$DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
rm -f $PIDFILE
echo "$DAEMON STOPED."
} do_reload() {
$DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
} do_status() {
ps aux|grep $DAEMON
} case "$1" in
status)
echo -en "Status $NAME: \n"
do_status
;;
start)
echo -en "Starting $NAME: \n"
do_start
;;
stop)
echo -en "Stopping $NAME: \n"
do_stop
;;
reload|graceful)
echo -en "Reloading $NAME: \n"
do_reload
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload}" >&
exit
;;
esac exit uwsgi8010

终端执行

-- 添加服务
chkconfig --add uwsgi9090
-- 设置开机启动
chkconfig uwsgi9090 on

四、本人接口的存放位置放在了/var/www目录下,包括uwsgi8010.ini和日志文件uwsgi8010.log

别外要注意的是

paste.request.parse_querystring(environ)
environ返回的是list,里面的参数是tulp,注意读取的方法和顺序