利用nohup命令创建启动服务

[root@PandoraBox:/mnt/sda1/projects/openwrtpytest/utils]#opkg -d usb install /mn
t/sda1/ipk/coreutils_8.16-1_ramips_24kec.ipk
Installing coreutils (8.16-1) to usb...
Configuring coreutils.
[root@PandoraBox:/mnt/sda1/projects/openwrtpytest/utils]#opkg -d usb install /mn
t/sda1/ipk/coreutils-nohup_8.16-1_ramips_24kec.ipk
Installing coreutils-nohup (8.16-1) to usb...
Configuring coreutils-nohup.

后面的&是后台运行
Linux shell中有三种输入输出,分别为标准输入,标准输出,错误输出,分别对应0,1,2

ps命令无法看到nohup的job,用jobs -l
 

用nohup执行python程序时,print无法输出
nohup python test.py > nohup.out 2>&1 &
发现nohup.out中显示不出来python程序中print的东西。
这是因为python的输出有缓冲,导致nohup.out并不能够马上看到输出。
python 有个-u参数,使得python不启用缓冲。
nohup python -u test.py > nohup.out 2>&1 &
 
创建启动服务

/etc/init.d/wechat

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org START=10
STOP=15 start() {
echo "wechat start" >> /mnt/sda1/temp/log
date -R >> /mnt/sda1/temp/log
nohup python -u /mnt/sda1/projects/openwrtpytest/utils/webhelper.py 2>>/mnt/sda1/temp/log 1>>/mnt/sda1/temp/log & # commands to launch application
} stop() {
echo "wechat stop" >> /mnt/sda1/temp/log
date -R >> /mnt/sda1/temp/log
# commands to kill application

  

chmod 777 /etc/init.d/wechat
启动

/etc/init.d/wechat start

注意wechat里面的\r\n要换成linux下的\n,不然会出错

如果重复启动后报错端口被占用,要先把原先的进程杀掉

error: [Errno 125] Address already in use
[root@PandoraBox:/mnt/sda1/projects/openwrtpytest/utils]#netstat -atpn | grep 8001
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 2626/python

kill 2626

 
webhelper.py
from wsgiref.simple_server import make_server
from wechat import app PORT = 8001
httpd = make_server('', PORT, app)
httpd.allow_reuse_address = True
httpd.serve_forever()

 wechat.py

#!/mnt/sda1/opkg/usr/bin/python
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import time
from flask import Flask, request,make_response,render_template
import hashlib
import xml.etree.ElementTree as ET
import loghelper
import traceback
import requests
import os
import pprint
import uniout
import apihelper
import yaml
import pickledb app = Flask(__name__)
app.debug=True
logger = loghelper.create_logger(r'/mnt/sda1/temp/log')
app_root = os.path.dirname(__file__)
templates_root = os.path.join(app_root, 'templates/reply_text.xml')
api = apihelper.ApiHelper()
cachefpath='../data/cache.db'
cachedb = pickledb.load(cachefpath,True) @app.route('/',methods=['GET','POST'])
def wechat_auth():
msg = "funcname : %s , request.args : %s" % (__name__ ,request.args)
logger.info(msg)
response = make_response("")
try:
if request.method == 'GET':
token='weixintest'
data = request.args
pprint.pprint( "Request get data:",data)
signature = data.get('signature','')
timestamp = data.get('timestamp','')
nonce = data.get('nonce','')
echostr = data.get('echostr','')
s = [timestamp,nonce,token]
s.sort()
s = ''.join(s)
if (hashlib.sha1(s).hexdigest() == signature):
response = make_response(echostr)
else:
response = make_response("Not Valid!")
else:
rec = request.stream.read()
print "Request post data:",rec
xml_rec = ET.fromstring(rec)
datadict = get_replydata(xml_rec)
pprint.pprint(datadict)
render = render_template("reply_text.xml",dict=datadict)
response = make_response(render)
#xml_rep = get_response(xml_rec)
#response = make_response(xml_rep)
response.content_type='application/xml;charset=utf-8'
except Exception,e:
type =xml_rec.find("MsgType").text
tou = xml_rec.find('ToUserName').text
fromu = xml_rec.find('FromUserName').text
datadict = {"to":fromu,"from":tou,"time":str(int(time.time())),"type":type,"msg":e.message}
render = render_template("reply_text.xml",dict=datadict)
response = make_response(render)
# log the exception
err = "exception : %s , %s" % (e.message ,traceback.format_exc())
logger.exception(err)
finally:
#pprint.pprint( request.__dict__)
pprint.pprint( response.__dict__)
return response def get_replydata(xml_rec):
'''
import xml2dict
xml = xml2dict.XML2Dict()
r = xml.fromstring(textdata)
r.get('xml').get('FromUserName').get('value')
'''
type =xml_rec.find("MsgType").text
tou = xml_rec.find('ToUserName').text
fromu = xml_rec.find('FromUserName').text
datadict = {"to":fromu,"from":tou,"time":str(int(time.time())),"type":type,"msg":""}
if type == 'image' or type == 'voice':
dmedia_id = xml_rec.find('MediaId').text
purl = xml_rec.find('PicUrl').text
fname = xml_rec.find('CreateTime').text
import urllib
urllib.urlretrieve(purl,'/mnt/sda1/data/image/%s.jpg' % fname)
print purl
elif type == 'location':
locx = xml_rec.find('Location_X').text
locy = xml_rec.find('Location_Y').text
scale = xml_rec.find('Scale').text
location = xml_rec.find('Label').text
wjdu="%s,%s" % (locx,locy)
data = api.location(wjdu)
datadict["msg"]=data
else:
content = xml_rec.find("Content").text
data = get_replytext(content)
datadict["msg"]=data
print datadict.get("msg")
return datadict def get_replytext(content):
content = content.strip()
print 'enter content :',content
cvalue = cachedb.get(content)
cvalue =None
if cvalue<>None:
print 'get it from cache'
data=cvalue
else:
if content.lower().startswith("ip"):
content=content.replace("ip","").strip()
data = api.ip(content)
elif content.startswith(u"航班"):
data=api.airline(content)
elif content.startswith(u"火车"):
data=api.train(content)
elif content.startswith(u"翻译"):
data=api.translator(content)
elif content.startswith(u"手机"):
data=api.mobile(content)
elif content.startswith(u"简体") or content.startswith(u"繁体"):
data=api.tradition(content)
elif content.startswith(u"汇率"):
data=api.huilv(content)
else:
data = api.weather(content)
cachedb.set(content,data)
return data if __name__ == '__main__':
path='../conf/test.yaml'
with open(path) as f:
x=yaml.load(f)
textdata = x.get('textdata')
wxlink = x.get('wxlink')
r=requests.get(wxlink)
print r.text
r=requests.post(wxlink,textdata)
print r.text

这里实现了查询航班,火车,汇率,中英文翻译,简繁体转换,查询当前IP,手机所在地的功能

代码放在github上
https://github.com/sui84/openwrtpytest

另外pandorbox上的python安装文件为了可以运行有些改动,放在这里

https://github.com/sui84/PandorboxPython27
 

Openwrt路由器上开发微信公众号应用的更多相关文章

  1. 苹果手机上开发微信公众号(wap)遇到的问题解决方法

    1.样式 苹果 手机会自行强制css样式,比如input会变得面目全非,你可以加上input{-webkit-appearance: none;}恢复原貌! 2.jquery的live错误,苹果手机上 ...

  2. NodeJs 开发微信公众号(五)真实环境部署

    在测试环境下开发完成代表着你离正式上线的目标不远了.接下来本章就主要谈一谈把测试环境的公众号升级为正式的公众号. 服务器和域名 目前为止我们只是在自己的电脑上完成了测试环境.真实的线上环境当然需要自己 ...

  3. vue+node.js+webpack开发微信公众号功能填坑——v -for循环

    页面整体框架实现,实现小功能,循环出数据,整体代码是上一篇 vue+node.js+webpack开发微信公众号功能填坑--组件按需引入 修改部门代码 app.vue <yd-flexbox&g ...

  4. vue+node.js+webpack开发微信公众号功能填坑——组件按需引入

    初次开发微信公众号,整体框架是经理搭建,小喽喽只是实现部分功能,整体页面效果 整个页面使用两个组件:布局 FlexBox,搜索框 Search,demo文档 http://vue.ydui.org/d ...

  5. 使用vue开发微信公众号下SPA站点的填坑之旅

    原文发表于本人博客,点击进入使用vue开发微信公众号下SPA站点的填坑之旅 本文为我创业过程中,开发项目的填坑之旅.作为一个技术宅男,我的项目是做一个微信公众号,前后端全部自己搞定,不浪费国家一分钱^ ...

  6. Java开发微信公众号(三)---微信服务器请求消息,响应消息,事件消息以及工具处理类的封装

    在前面几篇文章我们讲了微信公众号环境的配置 和微信公众号服务的接入,接下来我们来说一下微信服务器请求消息,响应消息以及事件消息的相关内容,首先我们来分析一下消息类型和返回xml格式及实体类的封装. ( ...

  7. Java开发微信公众号(二)---开启开发者模式,接入微信公众平台开发

    接入微信公众平台开发,开发者需要按照如下步骤完成: 1.填写服务器配置 2.验证服务器地址的有效性 3.依据接口文档实现业务逻辑 资料准备: 1.一个可以访问的外网,即80的访问端口,因为微信公众号接 ...

  8. Spring Boot 开发微信公众号后台

    Hello 各位小伙伴,松哥今天要和大家聊一个有意思的话题,就是使用 Spring Boot 开发微信公众号后台. 很多小伙伴可能注意到松哥的个人网站(http://www.javaboy.org)前 ...

  9. vue开发微信公众号--开发准备

    由于工作项目的原因,需要使用vue开发微信公众号,但是这种微信公众号更多是将APP套了一个微信的壳子,除了前面的授权和微信有关系以外,其他的都和微信没多大的关系了,特此记录 开发流程 首先需要在电脑上 ...

随机推荐

  1. 2019-10-11 ubuntu ssh远程免密登录配置及配置别名

    在客户端能正常远程访问服务端的前提下. 客户端: 1)配置免密 执行 ssh-keygen 即可生成 SSH 钥匙,回车三次. 执行 ssh-copy-id user@remote,可以让远程服务器记 ...

  2. String和Irreducible Polynomial(2019牛客暑期多校训练营(第七场))

    示例: 输入: 4000010010111011110 输出: 00001001 0111 01111 0 题意:给出一个只含有0和1的字符串,找出一种分割方法,使得每个分割出的字符串都是在该字符串自 ...

  3. fastjson反序列化使用不当导致内存泄露

    分析一个线上内存告警的问题时,发现了造成内存告警的原因是使用fastjson不当导致的. 分析dump发现com.alibaba.fastjson.util.IdentityHashMap$Entry ...

  4. AGC028E High Elements 贪心、DP、线段树

    传送门 看到要求"字典序最小"的方案,一个很直观的想法是按位贪心,那么我们需要check的就是当某一个数放在了第一个序列之后是否还存在方案. 假设当前两个序列的最大值和前缀最值数量 ...

  5. 系统开启UAC情形下开机自启动程序如何以管理员权限启动

    系统开启UAC情形下开机自启动程序如何以管理员权限启动 题记:本文阐述的是在Windows系统开启UAC的情况下,开机自启动程序需要以管理员权限启动, 系统弹出UAC对话框,用户同意的情形下启动程序 ...

  6. Maven过滤属性文件,替换属性值

    pom.xml 1.resources: resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean ...

  7. .Net MVC 输出HTML内容

    1.后台代码中的带HTML标记的内容 ViewData["msg"]="<b>Title</b>"; 然则如许打印出来的就是 <b ...

  8. mtd交叉编译mkfs命令

    下载 mtd:ftp://ftp.infradead.org/pub/mtd-utils/ zlib:http://www.zlib.net/ lzo:http://bouchez.info/lzo. ...

  9. [AIR] NativeExtension在IOS下的开发实例 --- Flex库项目的创建(二)

    来源:http://bbs.9ria.com/thread-102038-1-1.html 上一章,我已经介绍了如果创建IOS库文件,并定义了两个方法ShowIconBadageNumber和Init ...

  10. 【hadoop】在eclipse上运行WordCount的操作过程

    序:本以为今天花点时间将WordCount例子完全理解到,但高估自己了,更别说我只是在大学选修一学期的java,之后再也没碰过java语言了 总的来说,从宏观上能理解具体的程序思路,但具体到每个代码有 ...