網頁

2020年3月18日 星期三

https on nginx and python flask

建立 python flask 的網頁伺服器
$ cat server.py
@app.route('/api/PostTime', methods=['POST'])
def post_time():
    print(request.headers)
    print(request.json)
    result = '\n'.join([request.json['updDate'],
            request.json['camera']])
    print(result)
    return str(result)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port="5000", debug=True,
            # 下兩行可以啟動 https
            #ssl_context=("/home/user1/Data/webapi/openssl/web1/server.crt",
            #    "/home/user1/Data/webapi/openssl/web1/server.key")
            )

$ python3 server.py

$ cat PostTime.py
import requests
import os

data = {
    'updDate': '2019-01-02T15:10:11',
    'camera': 'A001',
}

os.environ['REQUESTS_CA_BUNDLE'] = '/home/user1/Data/webapi/openssl/ca/cacert.pem'
#url = 'https://127.0.0.1:5000/api/PostTime'
url = 'https://127.0.0.1:8443/api/PostTime'
headers = {'Content-Type': 'application/json'}
response = requests.post(url=url,
        headers=headers,
        #verify=False,
        json=data)
if response.ok:
    print("PostTime ok")
    print(response.status_code)
    print(response.text)
else:
    print(response.status_code)
    print(response.text)

$ python3 PostTime.py

# cat /etc/nginx/sites-available/config
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        listen 8443 ssl default_server;
        listen [::]:8443 ssl default_server;
        ssl_certificate /home/user1/Data/webapi/openssl/web1/server.crt;
        ssl_certificate_key /home/user1/Data/webapi/openssl/web1/server.key;
        root /var/www/html;

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

        server_name _;

        location ^~ /api/ {
                # 當 https 由 nginx 管控,python flask 就要走一般的 http
                proxy_pass http://127.0.0.1:5000;
                proxy_set_header Host $host;
        }
        # 設定上傳限制
        client_max_body_size 10M;
}

重新啟動 nginx
# sudo nginx -s reload

2020年3月12日 星期四

git & VS2017

在網站上 New Repository
VisualStudioTest

> mkdir VisualStudioTest
> cd VisualStudioTest
> git init
> git remote add origin http://ip:port/username/VisualStudioTest.git
> git remote -v

編輯 .git\config 將, 避免 push 時,出現 403
[remote "origin"]
url = http://192.168.0.101:8080/user/VisualStudioTest.git
改為
[remote "origin"]
url = http://mark1@192.168.0.101:8080/user/VisualStudioTest.git

開啟 VS2017
New Project
Location: VisualStudioTest 的上層目錄
Solution name: VisualStudioTest
Create directory for solution 要勾選

在 TeamExplorer 頁籤上 可以看到 VisualStudioTest
雙按 VisualStudioTest
選 Settings/Git/Repository Settings
按 Ignore & Attributes Files/Ignore File/Add
TeamExplorer /Sync
TeamExplorer /Changes
在 Changes 的目錄下按右鍵 選 Stage
輸入 commit
按 Commit Staged
按 Synchronization/Outgoing Commits/Push 上傳至網站


2020年3月11日 星期三

frp 應用

cp frps /opt/bin

frps.ini
[common]
bind_port = 7000
dashboard_port = 7001
dashboard_user = user
dashboard_pwd = password

用 nobody 開機自動執行
/etc/systemd/system/frps.service
[Unit]
Description=FRP Server Daemon

[Service]
Type=simple
ExecStartPre=-/usr/sbin/setcap cap_net_bind_service=+ep /opt/bin/frps
ExecStart=/opt/bin/frps -c /opt/etc/frps.ini
Restart=always
RestartSec=20s
User=nobody
PermissionsStartOnly=true
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

sudo systemctl start frps
sudo systemctl enable frps

frpc.ini
[common]
server_addr = digichance11.vigorddns.com
server_port = 7000
login_fail_exit = false

[vnc]
type = tcp
local_ip = 127.0.0.1
local_port = 8088
remote_port = 2172

開機自動執行
ftpc.vbs
set ws=Wscript.CreateObject("Wscript.Shell")
ws.Run "C:\frp_0.31.2_windows_386\frpc.exe -c C:\frp_0.31.2_windows_386\frpc.ini", 0
gpedit.msc
本機電腦 原則/電腦設定/指令碼 - (啟動/關機)
啟動/內容
新增 ftpc.vbs

frpc.bat
REM 雙擊執行,不顯示視窗
@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit

:begin
frpc.exe -c frpc.ini

連線 vnc 時,出現下列錯誤
Local loop-back connections are disabled.
在視窗右下角 vnc icon 按滑鼠右鍵/Admin Properties/滑鼠左鍵
打勾 Allow Loopback Connections


2020年3月3日 星期二

git 學習筆記 5, remote and github

git clone URL
預設遠端名為 origin
只抓取 master, 其餘 branch 不下載

git remote show <origin>
git remote add <name> URL
git remote rename <a> <b>

更改 remote
git remote set-url <name> URL

git branch -a
顯示所有 branch
git remote show origin
顯示 origin 資訊

參與修改 branch_a
git checkout origin/branch_a
切換到遠端的 branch_a
git checkout -b branch_a
在此建立一個同名的 branch

pull 範例,pull 等於 fetch + rebase
git fetch origin
取得遠端 origin 的更新, origin/master 往前走了
git checkout master
git rebase origin/master
將本地的更新插入 origin/master

git push <remote> <local branch>:<remote branch>
git push <remote> :<remote branch>
刪除 remote branch

fork on github
在 github 上產生 fork (需要有 github 的帳號)
$ git clone https://github.com:user/repository.git
clone 自己在 github 上產生的 fork repository
$ git remote add upstream https://github.com:ori_user/repository.git
下載 upstream 上的所有 branch
$ git fetch upstream
切換至 develop
$ git checkout develop
若本地的 develop 比 upstream/develop 慢
$ git rebase upstream/develop
$ git checkout -b fix_branch
更新開發端成最新
更改程式
$ git add your_modify
$ git commit
$ git push origin fix_branch:fix_branch
到 github 網頁(user) Repository
進入 Pull Requests, 按下 New Pull Request
選擇 merge into: develop ... pull from: fix_branch
按 Create pull request
到 github 網頁(ori_user)
按 pull request, 找到剛才的 pull request, 點選進入
要 merge, 按 Merge Pull Request
不要 merge, 按下 Close
$ git checkout develop
$ git fetch upstream
$ git rebase upstream/develop
$ git push origin develop
$ git push origin :fix_branch
$ git branch -d fix_branch

git 學習筆記 4, branch checkout merge

checkout 移動 HEAD
git checkout hash
移動 HEAD 到 hash commit
git checkout file
將檔案還原到 HEAD 狀態,放棄修改

預設的 branch 為 master

git branch
顯示所有 branch

git branch new_branch
新建一個 branch, 但 HEAD 沒有轉移,需要自己執行下列命令
git branch new_branch hash
在 hash commit 上建立 new_branch
git checkout new_branch
可用下列命令取代上兩個命令
git checkout -b new_branch

HEAD 必須和 branch 掛勾,
如 checkout 到舊的 commit, 若要以此為基準修改,則馬上執行
git checkout -b new_branch

git branch -d del_branch
刪除 branch
但刪除前需要 checkout 到別的 branch,不能刪除目前的 branch
刪除前會判斷目前的 branch 和要刪的 branch 是否需要 merge
若需要 merge, 則會失敗, 若不 merge 要直接刪,則用
git branch -D del_branch

git checkout branch_a
git merge branch_b
Fast-forward
git merge branch_b --no-ff
no Fast-forward
若有衝突(conflict), 可查看 git status
衝突解決用 git commit
放棄 merge, 使用 git merge --abort

vi conflict_file
<<<<<<< HEAD
在 HEAD 修改的內容
=======
在 branch_b 修改的內容
>>>>>>> branch_b

git mergetool
會顯示 branch_a, base_of_a_b, branch_b
還有要 merge 的檔

git add conflict_file
git commit


2020年3月2日 星期一

git 學習筆記 3

git hash 可以只使用 6 個字元

HEAD
Branch
Tag (版本)
Remote

git log --oneline
git log -n3
git log -n3 hash

git log HEAD^^
一個 ^ 表示往前一個, 兩個^ 表示往前兩個
git log HEAD~8
~8 表示往前8個

git log --oneline file
專門針對一個 file

git log --stat file
git log --patch file

git log -S token
git log --patch -S token
有修改 token 的地方

git diff
比較 unstage 和 HEAD 的差異
git diff --staged
比較 staged 和 HEAD 的差異
git diff hash file
比較 unstage 和 hash 版本的差異
git diff hash1 hash2
比較兩次 commit 的差異

git add -p
...
Stage this hunk [y,n,q,a,d,s,e,?]?
q: 離開
a: 加入全部
y: 加入
n: 不加入
s: 分段
K: 上一段
e: 編輯
移除 -, 將 '-' 換成 ' '
移除 +, 將整行刪除

git commit --amend
隱藏舊的 commit, 用新的 commit 取代

git 學習筆記 2

git commit message 有一個 50/72 法則
第一行部要超過 50字
其餘不要超過 72字

1. Issue
2. Problem, 如何重現問題
3. Cause
4. Fix

.gitignore
參考 GitHub gitignore

Git 檔案的四種狀態
1.未追蹤(Untracked files):在版本提交後才又加進來的檔案,這些檔案並沒有被GIT所追蹤控管
2.已更改(Changes not staged for commit):已提交版本後,卻又再次修改,這些檔案會被丟回工作目錄(WD)
3.等待提交(Changes to be committed):在工作目錄(WD)的檔案執行git add後,會放在暫存區(Stage)。這些放在暫存區的檔案狀態便是等待提交囉!不論檔案是新增,修改,或刪除整個檔案,都是用 git add。
4.已提交(Committed):在暫存區(Stage)的檔案執行git commit後,檔案便置於儲存區(Repo),這些放在儲存區的檔案即是已提交的狀態。

git reset HEAD file
將 file 由 stage 返回 unstage
git reset --hard HEAD
連檔案也回復,即修改消失