# 網路
# HTTP 超文本傳輸協議
## HTTP版本
* 0.9: request只能GET,傳輸內容為ASCII編碼為主
* 1.0: request增加多種方法(GET、POST、HEAD等)請求後斷線
* 1.1:
* reques請求後不斷線
* 有 pipeline 概念,但有Head-of-Line Blocking問題
* 有 chunk 概念
* 有 cache 的機制
* HTTP/2
* 向下相容1.1
* 改善Head-of-Line Blocking問題(利用Multiplexing)
* 最小化Protocol Overhead問題
* 說短網頁載入時間以及傳輸延遲
* 減少網站Connection數量
* Binary Protocol 以二進制方式傳輸
* Multiplexing 多工傳輸,不按順序傳輸回傳多個frame,藉由streamID來回傳對應請求
* Header Compress 使用HPACK壓縮演算法對header壓縮
* Server Push 自動推送client可能需要的資源
* HTTPS: 增加SSL安全超文字傳輸協定
## HTTP 基本常識
* 無狀態特性: 不同人不同電腦,只要知道相同HTTP Header 以及HTTP Methon都能得到相同內容
* 分散式架構:
* 協作的架構
* 超文本(not just text but with link)
## HTTP request methon
* GET 取得該伺服器資料的內容
* HEAD 取得該伺服器資料的相關訊息
* POST 向指定資源提交資料,請求伺服器進行處理
* PUT 向指定資源位置上傳其最新內容
* DELETE 請求伺服器刪除Request-URI所標識的資源
* TRACE 回顯伺服器收到的請求,主要用於測試或診斷。
* OPTIONS 這個方法可使伺服器傳回該資源所支援的所有HTTP請求方法。可以測試伺服器功能是否正常運作
* CONNECT
* PATCH 用於將局部修改應用到資源。
### HTTP cmd
```bash =
:~$ telnet 地址 port #建立TCP/IP連線
:~$ GET / (子地址 )HTTP/版本(1.0)
:~$ Host: 地址 #新增 HTTP header 前往網站
:~$ Range: byte=0-200(取得0-200byte的資料,為chunk的概念)
:~$ If-Modified-Since: (日期)
:~$ Accept-Language: en(語言)
:~$ Accept-encoding : "gzip, deflate, br"
:~$ Connection : keep-alive
```
## HTTP request header
* Host: 取得網址中的特定 domain
* Range: 取得資料中特定範圍的byte
* If-Modified-Since: client詢問如果這個文件在這日期後沒有修改的話,則繼續使用此文件,此時server回傳304(有開cache的前提下),以表示沒有修改
* Accept-Language: 只選擇給定語言的內容
## HTTP response status code
* 200: OK (伺服器成功回覆)
* 201: created (成功建立資料)
* 202: Accepted (交受要求,資料處理中,稍後再做請求)
* 3XX: 轉向
* 304: Not Modified
* 4XX: Client 錯誤
* 400: Bad Request
* 403: Forbidden(禁止用戶請求)
* 404: Not Found
* 406: Not Accepted
* 5XX: Server Error
* 500: Interal Server Error
* 502: Bad Gateway
* 503: Server Unavailable
# TCP/IP
## TCP/IP 分層
* physical later : ex.cabel, 電訊號
* data link layer : ex.MAC, 乙太網路, switch, 資料轉訊號
* network layer: ex.IP, routing
* transport layer: ex.TCP, UDP
* application layer: ex.HTTP
## TCP協議
* 基於連接
* 三次握手:

* 數據傳輸

* 四次揮手:

* 適合傳輸文件發送文件遊覽網頁
## UDP協議
* 基於非連接
* 單存傳輸數據包
* 校能消耗少、速度快、realtime
* 適合用於直播、通話、VPN
參考資料:[https://www.youtube.com/watch?v=Iuvjwrm_O5g&ab_channel=%E6%8E%8C%E8%8A%9D%E5%A3%AB](https://)
## IPV4
* 32位元,提供公2^32個固定IP
* 192.168.0.0-192.168.255.255為虛擬IP
## IPV6
* 由八個4位元16進制組成 3.4*10^38
## MAC
* 為網卡地址
## 網路遮罩MASK
* 將MASK (255.255.255.0) & IP地址 (192.168.1.1) = 192.168.1.0 用以判斷是否為同一子網
* TCP/IP協議中不同子網不能互相連線,當是不同子網時會將數居包的MAC地址改為GATEWAY的MAC地址
參考資料:[https://www.youtube.com/watch?v=tVNx-6OEy-k&ab_channel=%E7%A1%AC%E4%BB%B6%E8%8C%B6%E8%B0%88](https://)
API Python code
```python=
from flask import Flask, request, jsonify
from flask_cors import CORS
# import requests
app = Flask(__name__)
app.config["DEBUG"] = True
cors = CORS(app, resources={r"/*" : {"origins" : "*"}})
@app.route("/", methods=["GET"])
def print_something():
return "something"
@app.errorhandler(400)
def bad_request(error):
response = jsonify({'error': 'Bad Request'})
response.status_code = 400
return response
@app.route("/post", methods=["POST"])
def repeat():
data = request.get_json()
try:
sentence = data["sentence"]
return sentence
except Exception as e:
print(e)
raise e
# def call_api(sentence):
# import json
# import requests
# request_json = json.dumps({"sentence" : [sentence]})
# headers = {"Content-type" : "application/json"}
# r = requests.post('localhost:8001', data=request_json, headers=headers)
# processed_data = r.text
# return processed_data
if __name__ == "__main__":
from waitress import serve
from paste.translogger import TransLogger
serve(TransLogger(app, setup_console_handler=False),host="localhost", port=8001)
# serve(app, host="localhost", port=8001)
# app.run(host = "localhost",port = 8001, debug = True)
```
request Python code
```python=
def call_api(sentence):
import json
import requests
request_json = json.dumps({"sentence" : [sentence]})
headers = {"Content-type" : "application/json"}
r = requests.post('http://127.0.0.1:8001/post', data=request_json, headers=headers)
return json.loads(r.text)[0]
if __name__ == "__main__":
print(call_api("hello_world"))
```