web隨筆

# web初識:html

## 軟件開發架構:python

​ c/s架構:客戶端服務端架構mysql

​ b/s架構:瀏覽器與服務端web

本質:b/s架構其實也是c/s架構sql

## 什麼是web應用:數據庫

Web應用程序是一種能夠經過Web訪問的應用程序。Web應用程序的一個最大好處就是用戶很容易訪問應用程序。瀏覽器

用戶只要有瀏覽器便可,不須要再安裝其餘軟件。安全

web應用程序舉例:淘寶、天貓、新浪、搜狐服務器

### web程序的優勢:網絡

- 網絡應用程序強調瀏覽器的適用性。若是瀏覽器方沒有提供特定的功能,或者棄用特定的平臺或操做系統版本(致使不適用),就會影響大量用戶;
- 網絡應用依靠互聯網遠程服務器端的應用文件。所以,當鏈接出問題時,應用將不能正常使用。
- 許多網絡應用程序不是開源的,只能依賴第三方提供的服務,所以不能針對用戶定製化、個性化,並且大多數狀況下用戶不能離線使用,於是損失了不少靈活性;
- 它們徹底依賴應用服務商的可及性。若是公司倒閉,服務器中止使用,用戶也沒法追索之前的資料。對比而看,即便軟件製造商倒閉了,傳統的安裝軟件也能夠繼續運行,儘管不能再更新或有其餘用戶服務;
- 類似地,提供方公司對軟件和其功能有了更大的控制權。只要他們願意就能爲軟件添加新特性,即便用戶想等bugs先被解決再更新。跳過較差的軟件版本也不可能了。公司能夠強加不受歡迎的特性給用戶,也能夠隨意減小帶寬來削減開支。
- 公司理論上能夠檢索任何的用戶行爲。這有可能引發隱私安全問題。

### B/S架構優勢

瀏覽器/服務器架構(Browser/Server,簡稱B/S)可以很好地應用在廣域網上,成爲愈來愈多的企業的選擇。瀏覽器/服務器架構相對於其餘幾種應用程序體系結構,有以下3方面的優勢:

- 這種架構採用Internet上標準的通訊協議(一般是TCP/IP協議)做爲客戶機同服務器通訊的協議。這樣可使位於Internet任意位置的人都可以正常訪問服務器。對於服務器來講,經過相應的Web服務和數據庫服務能夠對數據進行處理。對外採用標準的通訊協議,以便共享數據。
- 在服務器上對數據進行處理,就處理的結果生成網頁,以方便客戶端直接下載。
- 在客戶機上對數據的處理被進一步簡化,將瀏覽器做爲客戶端的應用程序,以實現對數據的顯示。再也不須要爲客戶端單獨編寫和安裝其餘類型的應用程序。這樣,在客戶端只須要安裝一套內置瀏覽器的操做系統,直接安裝一套瀏覽器,就能夠實現服務器上數據的訪問。而瀏覽器是計算機的標準設備

**總結一下,本質上:瀏覽器是一個socket客戶端,服務器是一個socket服務端**

### 基於socket寫一個web應用

```python
import socket
def server_run():
sock = socket.socket()
sock.bind(("127.0.0.1",8888))
sock.listen(5)
while True:
conn,addr = sock.accept()
recv_data=conn.recv(1024)
#1直接在send裏寫發送的內容,發送到客戶端
conn.send(b"HTTP/1.1 200 OK\r\n\r\n<h1>hello</h1>")
#2打開html文件發送文件到客戶端
with open("index.html","r",encoding="utf-8") as f:
data=f.read()
conn.send(("HTTP/1.1 200 OK\r\n\r\n%s"%data).encode("utf-8"))
#3動態網頁,字符串替換
import time
now = time.strftime("%Y-%m-%d %X")
print(now)
with open("index.html","r",encoding="utf-8") as f:
data=f.read()
#將讀出來的字符串經過替換把時間加到html文件中
data = data.replace("@@@",now)
conn.send(("HTTP/1.1 200 OK\r\n\r\n%s"%data).encode("utf-8"))
```

 

```html
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>@@@</h2>

<img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=5e3814acf9edab64607f4592965fc4a6/14ce36d3d539b600c0c465d0eb50352ac65cb74b.jpg" alt="">
</body>
</html>
```

手擼一個web框架

```python
import socket
import pymysql


def index(request):
return 'index'


def login(request):
with open("login.html", "r", encoding="utf-8") as f:
data = f.read()
return data


def time(request):
import datetime
now = datetime.datetime.now().strftime("%Y-%m-%d %X")
with open("time.html", "r", encoding="utf-8") as f:
data = f.read()
data = data.replace("@@time@@", now)
return data


def user_list(request):
conn = pymysql.connect(
host="127.0.0.1",
port=3306,
user="root",
password="123",
database="day54",
charset="utf8",
autocommit=True,
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select id,name,password from user_info")
user_list = cursor.fetchall()
print(user_list)
tr_list = []
for user in user_list:
tr = "<tr><td>%s</td><td>%s</td> <td>%s</td></tr>" % (user["id"], user["name"], user["password"])
tr_list.append(tr)
with open("user_list.html", "r", encoding="utf-8") as f:
data = f.read()
data = data.replace("@@body@@", "".join(tr_list))
return data


def user_list_new(request):
from jinja2 import Template
conn = pymysql.connect(
host="127.0.0.1",
port=3306,
user="root",
password="123",
database='day54',
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
cursor.execute("select id,name,password from user_info;")
user_list = cursor.fetchall()
# for user in user_list:
cursor.close()
conn.close()
print(user_list)
with open("user_list_new.html", "r", encoding="utf-8") as f:
data = f.read()
template = Template(data)

response = template.render(user_list=user_list)
return response


urls = [
("/index", index),
("/login", login),
("/time", time),
("/user_list", user_list),
("/user_list_new", user_list_new),
]


def run():
sock = socket.socket()
sock.bind(("127.0.0.1", 8080))
sock.listen(5)
while True:
conn, port = sock.accept()
# 接收請求信息
data = conn.recv(1024)
print(data)
# data=str(data,encoding='utf-8')
data = data.decode("utf-8")

# b'GET / HTTP/1.1\r\nHost: 127.0.0.1:8080\r\n
# Connection: keep-alive\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5944.400 LBBROWSER/10.1.3378.400\r\nUpgrade-Insecure-Requests: 1\r\n
# Accept: text//html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\nAccept-Encoding: gzip, deflate, br\r\n
# Accept-Language: zh-CN,zh;q=0.9\r\n\r\n'
# 分割每條信息
request_list = data.split("\r\n\r\n")
head_list = request_list[0].split("\r\n")
method, url, http = head_list[0].split(' ')

conn.send(b"HTTP/1.1 200 OK\r\n\r\n")
print(url)
func_name = None
for u in urls:
if url == u[0]:
func_name = u[1]
break
if func_name:
response = func_name(data)
else:
response = "404 not found"
conn.send(response.encode("utf-8"))
conn.close()


if __name__ == '__main__':
run()

```

```html
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<form action="">
<p>用戶名:<input type="text"></p>
<p>密碼:<input type="password"></p>


</form>
</body>
</html>
```

```html

```

###### time.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>


@@time@@
</body>
</html>
```

 

user_list.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用戶列表</title>
</head>
<body>

<table border="1">
<thead>
<tr>
<th>id</th>
<th>用戶名</th>
<th>密碼</th>
</tr>
</thead>
<tbody>
@@body@@
</tbody>


</table>

</body>
</html>
```

user_list_new.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用戶列表</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
</tr>
</thead>
<tbody>

{% for user in user_list%}
<tr>
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.password}}</td>
</tr>
{%endfor%}


</tbody>


</table>

</body></html> ```

相關文章
相關標籤/搜索