280篇 Default中的文章

[Flask] multiple routes on same function 多个路由对应同一方法

事情是这样的,一个表单,我想把get和post请求放到一个方法里,但是get和post的路由不一样。

@app.route('/markdown', methods=['GET'])
@app.route('/page_create', methods=['POST'])
def create():
    form = PageForm()
    context = {
        'form': form
    }
...


<!--more-->


按理说可以这样搞,但是我希望是不同的路由进来。

@app.route('/markdown', methods=['GET', 'POST'])
def create():
    form = PageForm()
    context = {
        'form': form
    }
...

那么view里可以这样生成链接,其实就是url_for 多个参数,指定用的method

<form method="post" action="{{ url_for('markdown.create', _method='POST') }}">

但事后觉得这样写太乱了,所以最好还是将get和post请求分不同的方法来写。

More ~

[python] 使用密码连接redis服务器 Connect redis with password

第一种方法:

rds = redis.StrictRedis(host='localhost', port=6379, password='123456')

第二种方法:

redis_config = {
        'host': os.getenv('REDIS_HOST', '127.0.0.1'),
        'port': os.getenv('REDIS_PORT', 6379),
        'password': os.getenv('REDIS_PWD', '123456')
    }
redis_uri = 'redis://:{password}@{host}:{port}/0'.format(**redis_config)
redis = Redis.from_url(redis_uri)
More ~

alpine based image to docker compose get error: command 'gcc' failed with exit status 1

Error like this:

Collecting misaka==2.1.1 (from -r requirements.txt (line 12))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fa/87/b1020510a00aba1b936477e54180b143df654c565b84936b0b3e85272cf2/misaka-2.1.1.tar.gz (125kB)
    ERROR: Complete output from command python setup.py egg_info:
    ERROR: 
        No working compiler found, or bogus compiler options passed to
        the compiler from Python's standard "distutils" module.  See
        the error messages above.  Likely, the problem is not related
        to CFFI but generic to the setup.py of any Python package that
        tries to compile C code.  (Hints: on OS/X 10.8, for errors about
        -mno-fused-madd see http://stackoverflow.com/questions/22313407/
        Otherwise, see https://wiki.python.org/moin/CompLangPython or
        the IRC channel #python on irc.freenode.net.)
    
        Trying to continue anyway.  If you are trying to install CFFI from
        a build done in a different context, you can ignore this warning.
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.7/distutils/unixccompiler.py", line 118, in _compile
        extra_postargs)
      File "/usr/local/lib/python3.7/distutils/ccompiler.py", line 909, in spawn
        spawn(cmd, dry_run=self.dry_run)
      File "/usr/local/lib/python3.7/distutils/spawn.py", line 36, in spawn
        _spawn_posix(cmd, search_path, dry_run=dry_run)
      File "/usr/local/lib/python3.7/distutils/spawn.py", line 159, in _spawn_posix
        % (cmd, exit_status))
    distutils.errors.DistutilsExecError: command 'gcc' failed with exit status 1

And my Dockerfile is here:

More ~

PyMongo使用密码连接Mongodb

以前的写法是这样的,

from pymongo import MongoClient
client = MongoClient(host='127.0.0.1')
db = client['dbname']
db.authenticate(user, password)

不过上面的已经废弃

warning:: Starting in MongoDB 3.6, calling :meth:authenticate
invalidates all existing cursors. It may also leave logical sessions
open on the server for up to 30 minutes until they time out.

More ~

就在刚刚,又被面试官的mysql联合索引问题问倒了

MySql经常用,但是通常就是ORM中间件来CRUD操作,很少关注索引问题,可能是我近些年很少用MySQL做大型的系统了。
现在一面试就遇到所有领域的问题来都来一个问一遍,MySQL的复合索引其实也就一知半解。

面试官的问类似于select语句里where a>100 and b=3 and c=1 能否命中创建的(a, b, c)这种索引,还有a>100能否命中这个复合索引。

我想当然就说,a>100 and b=3 and c=1 只能命令(a, b, c)这种索引, a>100不能命中。唉呀,人家笑了。我说我可能说的不对,我下来试试。好吧查资料,建表,亲自实操一把。

More ~

Ubuntu Server 19.04配置静态IP

这个/etc/netplan下默认有个文件50-cloud-init.yaml,直接修改它就行了

sudo vim /etc/netplan/50-cloud-init.yaml

网口名字enp0s3可以通过ip a查到。 这个文件默认已经有网口名字了,将dhcp关闭,填上ip、网关、DNS地址:

network:
    ethernets:
        enp0s3:
            dhcp4: false
            addresses:[192.168.0.201/24]
            gateway4: 192.168.0.1
            nameservers:
                addresses: [192.168.0.1, 8.8.8.8]
    version: 2

保存后,执行 sudo netplan --debug apply 应用更新。
静态IP即可。

More ~

为Ubuntu Server开启SSH

虚拟机安装的Ubuntu Server镜像,向导里需要设置用户账号,没有设置root密码.

1.登陆初始账号,修改root 密码
sudo passwd root

su切换root 试试

2.root 账号下修改配置文件
vi /etc/ssh/sshd_config
找到下面相关配置:

# Authentication:
LoginGraceTime 120
PermitRootLogin prohibit-password
StrictModes yes

更改为:

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

3.重启ssh
sudo service ssh restart

More ~