ubuntu 16.04 uwsgi自启动

网上的关于uwsgi的自启动的方法还是挺多的,具体搜索一下就知道了,这里简单的写一下官方推荐的方法,通过systemd启动服务。如果用这个方法需要首先确定systemd的版本大于211。

通过下面的命令获取systemd版本信息:

root@mars:/etc/systemd/system# systemctl --version
systemd 229
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN

按照官方的文档编写service文件,放入/etc/systemd/system目录下然后执行

systemctl start emperor.uwsgi.service即可启动服务。service文件如下:
[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --ini /var/www/html/project/uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

这个脚本需要注意两个地方,一个是uwsgi的可执行文件路径,另外一个是uwsgi.ini配置文件路径。可执行文件路径可以通过whois uwsgi获取。

Continue Reading

django 主动抛出 403 异常

网上的做法基本都是下面的代码

return HttpResponseForbidden()

试了一下,效果一般,没有异常页面显示,最终显示的是浏览器的异常页面,如下图:

如果要想让服务器截获异常并且显示错误页可以用下面的方式:

id = request.GET.get('id', '')
timestamp = request.GET.get('timestamp', '')
accesskey = request.GET.get('accesskey', '')

if timestamp == '' or accesskey == '' or id == '':
    raise PermissionDenied

Continue Reading

如何绕过微信图片的防盗链

具体实现原来可以参考这个链接: https://www.zhihu.com/question/35044484

下面给个Django下的实现代码:

@csrf_exempt
def image_proxy(request):
    img = request.GET.get('img')
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
    }
    status = 0
    try:
        r = requests.get(
            img,
            headers=headers)
    except ConnectionError, ConnectTimeout:
        status = 1
    if status == 1:
        return ''
    response = HttpResponse(r.content, content_type='image/jpeg')
    return response

url.py

url(r'^spider-api/image-proxy/$', image_proxy),

访问方法,url:

http://127.0.0.1:8001/spider-api/image-proxy/?img=https://mmbiz.qpic.cn/mmbiz_png/WliaoSKPrpSPqGrhMmQK8MwKR6AZ7qDDy2JtSxRjk3ZUke41PUGP6RoaibzIgxw8ey5cejb5FzkplhgGd48oOxAg/640

获取网页中所有的文字

# encoding=utf8

import sys

reload(sys)

sys.setdefaultencoding('utf8')

import re
import requests
from bs4 import BeautifulSoup


html = requests.get('https://mp.weixin.qq.com/s?src=11×tamp=1533887718&ver=1051&signature=Xszdx5nmmHyebcH0MXxyHi7-jDwGoNDUDXCHJzPVic68tXGRSTiM3CStUDfSR*aALaC3nK3Ez4e33uLR5ir1pLgy3vEvWXWOvVXgAbsXMn5fB-HWboOW26GH*KMRVhgX&new=1')
soup = BeautifulSoup(html.text, "html5lib")
data = soup.findAll(text=True)


def visible(element):
    if element.parent.name in ['style', 'script', '[document]', 'head', 'title']:
        return False
    elif re.match('', str(element.encode('utf-8'))):
        return False
    return True


result = filter(visible, data)

with open('res.txt', "w+") as p:
    for i in result:
        print(str(i))
        p.write(str(i))


print list(result)

Broadlink-e-control-db-dump bug fix

This script will “parse” the broadlink e-Control Android application **rmt.db database** and dump the IR / RF codes (in HEX format) for selected accessories into a text file which can be later used with broadlink-python to send the codes to the RM PRO hub.

这个东西在处理逻辑上有bug,如果出现查询为空的情况,就挂了。

Continue Reading

domoticz 检测设备在线状态

最近又开始折腾语音控制相关的东西,比较郁闷的是家里的电视机和机顶盒貌似并不能直接接入。于是每次想要开关电视和机顶盒就需要知道设备的在线状态。那么为了获取在线状态,最简单的办法就是创建一个开关,标记当前设备的状态:

设备类型如下:

如果不能手工添加开关,现在硬件页面创建一个Dummy类型的设备,新增加的开关类型为Dummy。

Continue Reading