python, linux, 前端,實踐過程當中的短知識總結(入門級)(長期更新)

1.sqlalchemy不自帶表更新,官方的遷移工具主要是 alembicsqlalchemy migrate, 若是不使用工具,建立新表能夠更新model後經過sqlalchemycreate_all方法,修改表字段則只能進數據庫手動改,同時更新model。
另外, sqlite在shell下執行sql腳本的方式是:sqlite my.db < test.sql
(在客戶端命令行中,是:.read test.sqljavascript


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.java


3.jquery$(function () {})寫法,意思至關於$(document).ready(function()),即文檔加載完再執行。python

摘:$(function () {})是$(document).ready(function())的簡寫,也至關於window.onload = function(){ }。雖然這段jquery代碼與javascript代碼在功能上能夠互換,但執行的時間不同,前者頁面框架加載完成就執行,後者頁面中全部內容加載完成才執行jquery


4.有的系統(好比debian)默認用戶的命令環境是sh,而不是bash,致使命令行用起來不正常。
打開/etc/passwd,看到好比root:x:0:0:root:/root:/bin/sh,即定義root用戶默認使用sh,而咱們通常使用的是bash。改爲/bin/bash便可sql

改成bash後,‘#'號前面的文字有時候是:-bash-4.2#,這個是由一個環境變量PS1決定的。這種狀況下執行一下echo $PS1,輸出是相似\s-\v\$
要顯示成常見的相似[user@localhost ~]狀態,能夠改成[\u@\h \w]\$shell


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後綴的不會執行…django


7.sqlalchemy 報錯:TypeError: an integer is required,這種狀況通常是有數據項的值不符合數據類型,但不必定跟整型有關,好比我就是給boolean類型的字段賦了個字符串的值就出現了這個報錯。另外sqlite裏boolean的值顯示出來是0和1.json


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的差異
一圖流……
clipboard.png


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

相關文章
相關標籤/搜索