pxchen110
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    伺服器(server)根據[維基百科定義](https://zh.wikipedia.org/zh-tw/%E6%9C%8D%E5%8A%A1%E5%99%A8#%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%88%86%E7%B1%BB%EF%BC%88%E8%BD%AF%E4%BB%B6%EF%BC%89),為提供服務的電腦軟體,根據軟體種類可分為: - 檔案伺服器: NAS儲存裝置... - 網頁伺服器: Apache... - 資料庫伺服器: MySQL, MongoDB... 建置正式的伺服器需要諸多考量和維護;如果想檢查網頁專案的目前成果,或簡單分享電腦資源給其他協作者的話,可以透過python建立臨時的網頁伺服器。 在指定目錄內包含`index.html`就能預覽網頁,若沒有則會顯示當前資料夾結構。以下使用 `http.server` 模組建立靜態網頁伺服器 ## http.server 產生靜態伺服器 **注意:Python2 和 Python3 使用的模組不同,這裡介紹的為適用於Python3套件與使用方式** ### 下載並引入套件 ``` pip install httpserver import http.server from http.server import SimpleHTTPRequestHandler, HTTPServer ``` 還需多引入兩個物件: `SimpleHTTPRequestHandler` 和 `HTTPServer` 下面會簡單介紹他們的功能 ### 最簡單的建置伺服器程式碼 ``` ## example code to create and run the server from python docs def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler): server_address = ('', 8000) httpd = server_class(server_address, handler_class) httpd.serve_forever() ``` ### HTTPServer 和 SimpleHTTPRequestHandler `HTTPServer` 為socketserver.TCPServer的子類別,能建立一個伺服器實例 還需要給個 `RequestHandlerClass` 向HTTP提出請求與回應,有以下三種: - BaseHTTPRequestHandler - SimpleHTTPRequestHandler: 讀取與發佈當前目錄 (比較多人用此object) - CGIHTTPRequestHandler 最後給個協定版本(protocol) ``` HandlerClass = SimpleHTTPRequestHandler ServerClass = HTTPServer Protocol = "HTTP/1.0" ``` :star2: Handler 是什麼? ![image](https://hackmd.io/_uploads/HJxOqrw5C.png) 圖片引用自: https://blog.csdn.net/weixin_62079735/article/details/132797418 輸入伺服器相關資訊,包含ip位址,指定port(預設8000)等,並存為`httpd`變數 ``` if sys.argv[1:]: port = int(sys.argv[1]) else: port = 8000 server_address = ('127.0.0.1', port) HandlerClass.protocol_version = Protocol ## standard formula: ip + port + handlerclass httpd = ServerClass(server_address, HandlerClass) ``` 實體化與運作伺服器 `serve_forever()`可以透過 `shutdown()` 關閉 ``` sa = httpd.socket.getsockname() ## ('127.0.0.1' 8000) print ("Serving HTTP on", sa[0], "port", sa[1], "...") httpd.serve_forever() ``` console 顯示 GET ... 200 代表請求成功 ``` Serving HTTP on 127.0.0.1 port 8000 ... 127.0.0.1 - - "GET / HTTP/1.1" 200 - ``` 最後打開瀏覽器,輸入 `http://127.0.0.1:8000/` 或是 `http://${your_ip}:8000/` 就可看到成果了 ### 完整 co ``` # pip install httpserver import sys import http.server from http.server import SimpleHTTPRequestHandler, HTTPServer HandlerClass = SimpleHTTPRequestHandler ServerClass = HTTPServer Protocol = "HTTP/1.0" if sys.argv[1:]: port = int(sys.argv[1]) else: port = 8000 server_address = ('127.0.0.1', port) HandlerClass.protocol_version = Protocol httpd = ServerClass(server_address, HandlerClass) sa = httpd.socket.getsockname() ## ('127.0.0.1' 8000) print ("Serving HTTP on", sa[0], "port", sa[1], "...") httpd.serve_forever() ## it will print 127.0.0.1 - - "GET / HTTP/1.1" 200 - on console ## <-> to shutdown() ``` ### 觀看 html 效果 拿出之前寫的index.html 並放在指定路徑,透過 httpserver 產生 localhost 查看 html 網頁,若沒有 html 會顯示本機資料夾結構,可以透過這個方式與人分享網址,查看本機儲存的檔案內容 ## 動態伺服器與網路框架 網頁包含動態資訊如資料庫串接,自定義的網頁跳轉等,可利用python `flask` 達成。此模組提供伺服器端的網頁框架,協助高效率架設網站。 ### 下載與引入套件 這裡先引入最基本的 `Flask` 其他常用函數(`redirect, url_for, send_from_directory...`)會在後面介紹 ``` #pip install Flask from flask import Flask ``` ### 簡單的範例 - create `app.py` ``` # save this as app.py from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello, World!" if __name__ == '__main__': app.debug = True ## not shown in formal case app.run() ``` - 在 `app.py` 同層路徑下執行 `flask run` flask 會生成網頁於 http://127.0.0.1:5000/ ### 名詞和概念筆記 - flask 內含有 httpserver,但僅適合測試用,正式發布會建議用 **WSGI server** - 裝飾器(decorator): `@app.route` 做為路由,紀錄 URL/HTTP動作 與其對應的處理函式 - `@app.route('/')` 這裡的 `/` 代表 http://127.0.0.1:5000/ 這個URL - 若連結到此網址,會執行 hello() 函數並在網頁印出 Hello, World! - 若今天還有其他路由如下,打開 http://127.0.0.1:5000/**page/5** 就會看到函式執行結果 ``` app.route('/page/5') def welcome(): return "Welcome" ``` - flask 可以吃URL變數,方法為 `<variable_name>` ### flask 函式 `url_for` `redirect` `url_for()` 可以讓路由具有追蹤彈性,並搭配 `redirect()` 跳轉網頁 下面例子可知: http://127.0.0.1:5000/b 會被引導回 http://127.0.0.1:5000/ ``` from flask import url_for, redirect @app.route('/') def hello(): return 'hello' @app.route('/b') def b(): return redirect(url_for('hello')) ``` 其他套件如結合 html css 模板的`render_template`,或是上傳下載檔案的 `send_from_directory` 會在下篇介紹我的side project時補上~ ## References https://docs.python.org/3/library/http.server.html# https://chwang12341.medium.com/coding%E8%B5%B7%E4%BE%86-python-%E4%B8%80%E8%A1%8C%E6%8C%87%E4%BB%A4%E5%B0%B1%E8%83%BD%E8%BC%95%E9%AC%86%E5%BB%BA%E7%AB%8B%E7%B6%B2%E9%A0%81%E4%BC%BA%E6%9C%8D%E5%99%A8-simplehttpserver%E5%A5%97%E4%BB%B6-http-server%E4%BD%BF%E7%94%A8%E6%95%99%E5%AD%B8-34c30b81c26 https://hackmd.io/@peterju/B18gmJ7Ph#15Flask-%E7%B6%B2%E9%A0%81%E6%A1%86%E6%9E%B6 https://medium.com/@charming_rust_oyster_221/%E4%BD%BF%E7%94%A8-flask-%E5%89%B5%E5%BB%BA-web-api-%E7%AD%86%E8%A8%98-b5618543632e https://medium.com/@charming_rust_oyster_221/flask-%E6%AA%94%E6%A1%88%E4%B8%8A%E5%82%B3%E5%88%B0%E4%BC%BA%E6%9C%8D%E5%99%A8%E7%9A%84%E6%96%B9%E6%B3%95-1-c11097c23137 https://hackmd.io/@shaoeChen/HJiZtEngG/https%3A%2F%2Fhackmd.io%2Fs%2FSyP4YEnef

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully