Raspberry配置nginx

1、安装nginx。

>> sudo apt install nginx

2、修改主配置文件。

>> sudo emacs /etc/nginx/nginx.conf
user pi;
C-x C-s
C-x C-c

3、修改网站配置文件。

>> sudo emacs /etc/nginx/sites-available/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /home/pi/websites/homebankweb/homebank/dist;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name 192.168.1.254;

        location / {
                root /home/pi/websites/homebankweb/homebank/dist;
                index index.html;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
        {
            root /home/pi/websites/homebankweb/homebank/dist;
            expires      7d;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

4、让网站配置文件生效。

>> sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
>> sudo service nginx restart

Raspberry设置静态IP

1、编辑网络配置文件。

>> sudo vi /etc/network/interfaces
iface eth0 inet static
address 192.168.1.254
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 114.114.114.114
>> sudo service networking restart

2、重启系统和路由器。

如果不行的话,试试把路由器也设置成静态地址绑定……

小家手帐网页版基本完成

利用不到一个月的周末业余时间,使用刚刚接触不久的vuejs,基本完成了小家手帐的网页版。

代码发布到了 https://github.com/codinglonglong/homebankservicehttps://github.com/codinglonglong/homebankweb 这两个代码仓库。

服务端主要涉及的相关技术点包括:

  1. flask_restplus实现的RESTful服务;
  2. sqlalchemy实现的数据库ORM;
  3. TimedRotatingFileHandler实现的滚动日志;
  4. 自定义装饰器实现的权限验证;
  5. “接口-接口模型-数据库访问-数据库模型”结构网站的探索;
  6. Swagger-UI接口测试的使用。

网页端主要涉及的相关技术点包括:

  1. 基于vue-router的页面导航;
  2. 基于axios和vue-axios的前后端通信;
  3. 基于element-ui的页面交互;
  4. 基于vue-cookie的cookie操作;
  5. vuejs中多组件的通信。

Flask-CORS解决Ajax跨域请求问题

在本地开发网站的过程中,js访问后端服务会出现

已拦截跨源请求:同源策略禁止读取位于 http://192.168.99.100:8080/signup/ 的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。

这时可以通过Flask-CORS解决问题。

1、安装。

pip install flask-cors

2、使用。

1
2
3
from flask_cors import CORS
global_app = Flask(__name__)
CORS(global_app)

Python装饰器探索

Python的装饰器是一个很有趣的语法糖,它可以让代码变得简洁优美,将与逻辑无关的部分分离出来,比如记录日志、验证权限、设置缓存、程序计时、配置上下文等等。

1、带参数函数、无参数装饰器

# -*-coding:utf-8-*-
# 带参数函数、无参数装饰器


def checklogin(func):
    def _(username):
        validusers = ["long", "jack"]
        if username in validusers:
            return func(username)
        else:
            return str(username) + ", 你没有操作权限"
    return _


@checklogin
def login(username):
    return "欢迎你, " + str(username)


print(login("long"))
print(login("tom"))
print(login("jack"))

运行结果:

欢迎你, long
tom, 你没有操作权限
欢迎你, jack

2、可变参数函数、无参数装饰器

# -*-coding:utf-8-*-
# 可变参数函数、无参数装饰器


def logger(func):
    def _(*args, **kwargs):
        print(str(func.__name__) + '被调用,参数包括:' + str(args) + str(kwargs))
        if args and kwargs:
            return func(args, kwargs)
        elif args:
            return func(args)
        elif kwargs:
            return func(kwargs)
        else:
            return func()
    return _


@logger
def test1():
    pass


@logger
def test2(args):
    pass


@logger
def test3(kwargs):
    pass


@logger
def test4(args, kwargs):
    pass


test1()
test2("long")
test3(a=1, b=2)
test4("long", a=1, b=2)

运行结果:

test1被调用,参数包括:(){}
test2被调用,参数包括:('long',){}
test3被调用,参数包括:(){'a': 1, 'b': 2}
test4被调用,参数包括:('long',){'a': 1, 'b': 2}

3、带参数函数、带参数装饰器

# -*-coding:utf-8-*-
# 带参数函数、带参数装饰器


def typechecker(paramtype):
    def _outer(func):
        def _inner(*args):
            for a in args:
                if type(a) is not paramtype:
                    args = [str(v) for v in args]
                    return "+".join(args) + " 类型错误"
            return func(args)
        return _inner
    return _outer


@typechecker(int)
def test(value):
    result = sum(value)
    value = [str(v) for v in value]
    return "+".join(value) + '=' + str(result)


print(test(1, 2, 3, 4, 5))
print(test(1, 2, 3, 'a', 5))

运行结果:

1+2+3+4+5=15
1+2+3+a+5 类型错误

4、带参数函数、可变参数装饰器

# -*-coding:utf-8-*-
# 带参数函数、可变参数装饰器


def typechecker(*paramtype):
    def _outer(func):
        def _inner(*args):
            for a in args:
                if type(a) not in paramtype:
                    return "参数不是有效的类型"
            return func(args)
        return _inner
    return _outer


@typechecker(int, str, float)
def test(value):
    return "参数有效"


print(test([1, 2], 5, 5.1, "a"))
print(test(1, 2, 3, 5.2, 'a', 5))

运行结果:

参数不是有效的类型
参数有效

5、多个装饰器的调用顺序

# -*-coding:utf-8-*-
# 多个装饰器的调用顺序


def deco1(func):
    def _():
        print("deco1() is called.")
        return func()
    return _


def deco2(func):
    def _():
        print("deco2() is called.")
        return func()
    return _


@deco1
@deco2
def test():
    print("test() is called.")


test()
test()

运行结果:

deco1() is called.
deco2() is called.
test() is called.
deco1() is called.
deco2() is called.
test() is called.