python, linux, 前端,实践过程当中的短学问总结(入门级)(历久更新)

1.sqlalchemy不自带表更新,官方的迁徙东西主假如 alembicsqlalchemy migrate, 假如不运用东西,建立新表能够更新model后经由过程sqlalchemycreate_all要领,修正表字段则只能进数据库手动改,同时更新model。
别的, sqlite在shell下实行sql剧本的体式格局是:sqlite my.db < test.sql
(在客户端敕令行中,是:.read test.sql

2.flask援用设置文件时,有几种要领,参考链接:https://zhuanlan.zhihu.com/p/…

重要要注重的是设置文件中,设置项必需以大写情势,不然app.config的几个读设置文件的要领(from_object, from_pyfile等)都不会认小写的变量,官方文档申明以下:
The configuration files themselves are actual Python files. Only values in uppercase are actually stored in the config object later on. So make sure to use uppercase letters for your config keys.

3.jquery$(function () {})写法,意义相当于$(document).ready(function()),即文档加载完再实行。

摘:$(function () {})是$(document).ready(function())的简写,也相当于window.onload = function(){ }。虽然这段jquery代码与javascript代码在功能上能够交换,但实行的时候不一样,前者页面框架加载完造诣实行,后者页面中一切内容加载完成才实行

4.有的体系(比方debian)默许用户的敕令环境是sh,而不是bash,致使敕令行用起来不正常。
翻开/etc/passwd,看到比方root:x:0:0:root:/root:/bin/sh,即定义root用户默许运用sh,而我们平常运用的是bash。改成/bin/bash即可

改成bash后,‘#’号前面的笔墨有时候是:-bash-4.2#,这个是由一个环境变量PS1决议的。这类状况下实行一下echo $PS1,输出是相似\s-\v\$
要显现成罕见的相似[user@localhost ~]状况,能够改成[\u@\h \w]\$

5.几个手艺名词:

`raid  ->  disk`
`ipvs  ->  host   (loadbalancing)`
`vrrp  ->  router  (high-ability)`
`bond  ->  network card`
`(lvs   =  ipvs + vrrp)`
简朴明白:
raid就是把几块硬盘连在一起,到达进步存储稳定性的结果,对存储者来讲像是一块硬盘。
ipvs就是把几台服务器主机合在一起,重要用于负载平衡,对客户端接见者来讲像是一台服务器。
vrrp就是多个路由器合在一起,重要用于高可用,对主机来讲就像是一个路由器。
bond就是多个网卡合在一起,既可用于高可用也可用于负载平衡,对主机来讲就像是一张网卡。
实在无非是把多个对象对外表现为一个,完成负载平衡或许高可用。固然细节就比较复杂了。

6.debian系下cron默许运用run-parts敕令实行小时、天、周等定时,man一下可知此敕令用于实行一个文件夹内一切可实行文件,然则ln了一个软链到/etc/cron.hourly目次下发明并不会实行,手动实行run-parts也确切没有输出,本来是因为run-parts这个敕令,关于带.sh后缀的不会实行…

7.sqlalchemy 报错:TypeError: an integer is required,这类状况平常是有数据项的值不符合数据范例,但不肯定跟整型有关,比方我就是给boolean范例的字段赋了个字符串的值就涌现了这个报错。别的sqlite里boolean的值显现出来是0和1.

8.关于flask上下文:
之前用django,习惯了进python敕令行,class request():DATA={}手动编一个要求,然后直接实例化view对象,调对应函数把request传进去即模拟了一次要求。

flask中要求并不直接作为参数通报到相应的函数中,而是作为一个上下文环境中的相似全局变量(彷佛差别于一般全局变量,待继承进修)。因而,在flask中要完成相似django那样敕令行交互,须要些特别处置惩罚,参考 http://flask.pocoo.org/docs/0…

这里简朴讲下流程: main.py是我的view文件,个中实例化了Flask对象(app),get_all是个中一个相应要求的函数。

>>> from main.main import *
>>> d=app.test_request_context('shiki/?shiki=hahahahahaha')
>>> print(get_all().get_data())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\workbench\flasksite\main\main.py", line 68, in get_all
    db = get_db()
  File "E:\workbench\flasksite\main\main.py", line 80, in get_db
    if not hasattr(g, 'db'):
  File "C:\Program Files\Python36\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Program Files\Python36\lib\site-packages\werkzeug\local.py", line 306, in _get_current_object
    return self.__local()
  File "C:\Program Files\Python36\lib\site-packages\flask\globals.py", line 44, in _lookup_app_object
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in a way.  To solve
this set up an application context with app.app_context().  See the
documentation for more information.
>>> d.push()
>>> print(get_all().get_data())
b'[\n  "hahahahahaha"\n]\n'
>>>

能够看到直接实行get_all是会提醒Working outside of application context。建立一个test_request_context对象,传入url作为参数,并实行push,就相当于进入了上下文环境,能够实行了。d.pop可退出上下文环境。

ps:看起来比升引postman彷佛毫无上风,不过有时候就是双手不想脱离键盘… (:з」∠)

9.python2, 规范库urllib(2)和三方库requests的差异
一图流……
《python, linux, 前端,实践过程当中的短学问总结(入门级)(历久更新)》

10.关于nfs设置

no_root_squash:登入 NFS 主机运用分享目次的运用者,假如是 root 的话,那末关于这个分享的目次来讲,他就具有 root 的权限!这个项目『极不平安』,不发起运用! 
root_squash:在登入 NFS 主机运用分享之目次的运用者假如是 root 时,那末这个运用者的权限将被紧缩成为匿名运用者,一般他的 UID 与 GID 都邑变成 nfsnobody 谁人体系账号的身份。

11.关于原生django(2.0.4)rest_framework
rest_framework中,封装了data属性,能够由这个一致接口拿到差别要求范例的数据。官方文档以下:

request.data returns the parsed content of the request body. This is

similar to the standard request.POST and request.FILES attributes

except that:

· It includes all parsed content, including file and non-file inputs.
· It supports parsing the content of HTTP methods other than POST,
meaning that you can access the content of PUT and PATCH requests.
· It supports REST framework’s flexible request parsing, rather than
just supporting form data. For example you can handle incoming JSON
data in the same way that you handle incoming form data.

django中,request对象有request.GETrequest.POST要领离别用于猎取数据,但POST不能猎取application/json范例的要求体。
json数据只能在request.body属性中拿到,是一个bytes范例(python3),须要json.loads转为字典。
没有一个像rest_framework那样的一致接口拿种种要求范例的数据。假如要做到那样,一种解决办法是和前端商定要求体悉数用json,然后写个中间件。相似:

try:
    request.DATA = json.loads(request.body)
except json.decoder.JSONDecodeError:
    xxx

注重中间件不该该去接见request.POST.
Accessing request.POST inside middleware before the view runs or in process_view() will prevent any view running after the middleware from being able to modify the upload handlers for the request, and should normally be avoided.
固然也能够get和post明白各自用,json再别的一个接口,关于这一点彷佛没有找到作风指南。直觉来讲肯定是一致接口轻易。

我的状况是公司底本项目用了rest_framework,而我近来的任务要撸个自力的体系,我盘算纯用django。关于我们为何要运用rest_framework,见:
When should i use django-rest-framework?

11.关于gevent.subprocess.Popen
gevent.subprocess.Popen运用数组作为参数(shell=False)时元素必需都为字符串范例,不然会抛出非常— execv() arg 2 must contain only strings

    原文作者:nifhlheimr
    原文地址: https://segmentfault.com/a/1190000013767626
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞