14-1

socket server

import socket

def handle_request(client):
    buf = client.recv(1024)
    client.send(bytes('HTTP/1.1 200 OK

', encoding='utf-8'))
    client.send(bytes('Hello, Seven', encoding='utf-8'))

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 8000))
    sock.listen(5)

    while True:
        connection, address = sock.accept()
        handle_request(connection)
        connection.close()

if __name__ == '__main__':
    main()

运行socket server, 用浏览器打开http://localhost:8000/

socket server返回结果修改

import socket

def handle_request(client):
    buf = client.recv(1024)
    client.send(bytes('HTTP/1.1 200 OK

', encoding='utf-8'))
    client.send(bytes("<h1 style='background-color:red;'>Hello, Seven<h1>", encoding="utf-8"))

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 8000))
    sock.listen(5)

    while True:
        connection, address = sock.accept()
        handle_request(connection)
        connection.close()

if __name__ == '__main__':
    main()

运行socket server, 用浏览器打开http://localhost:8000/

 socket server从文件中读取内容返回给客户端

import socket

def handle_request(client):
    buf = client.recv(1024)
    client.send(bytes('HTTP/1.1 200 OK

', encoding='utf-8'))
    f = open('index.html', 'rb')
    data = f.read()
    f.close()
    client.send(data)

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 8000))
    sock.listen(5)

    while True:
        connection, address = sock.accept()
        handle_request(connection)
        connection.close()

if __name__ == '__main__':
    main()

编辑index.html

<h1 style='background-color:red;'>Hello, Seven</h1>
<a href='https://www.oldboyedu.com'>oldboy</a>
<table border='1'>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
</table>

运行socket server, 用浏览器打开http://localhost:8000/

替换html模板中的内容

import socket

def handle_request(client):
    buf = client.recv(1024)
    client.send(bytes('HTTP/1.1 200 OK

', encoding='utf-8'))
    f = open('index.html', 'r', encoding='utf-8')
    data = f.read()
    f.close()
    import time
    r = str(time.time())
    data = data.replace('@@@@@', r)
    client.send(bytes(data, encoding='utf-8'))

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 8000))
    sock.listen(5)

    while True:
        connection, address = sock.accept()
        handle_request(connection)
        connection.close()

if __name__ == '__main__':
    main()

编辑index.html

<h1 style='background-color:red;'>@@@@@</h1>
<a href='https://www.oldboyedu.com'>oldboy</a>
<table border='1'>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
</table>

运行socket server, 用浏览器打开http://localhost:8000/

HTML

HTML
    1、一套规则,浏览器认识的规则
    2、开发者:
        学习HTML规则
        开发后台程序:
            - 写HTML文件(充当模板的作用)
            - 数据库获取数据,然后替换到HTML文件的指定位置(Web框架)
    3、本地测试
        - 找到文件路径,直接浏览器打开
        - pycharm打开测试
    4、编写HTML文件
        - doctype对应关系 <!DOCTYPE html>
        - html标签,标签内部可以写属性
        - 注释: <!-- 注释的内容 -->        
    5、标签分类
        - 自闭合标签
            <meta charset="UTF-8">
        - 主动闭合标签
            <title>老男孩</title>
    6、head标签中
        - <meta > 编码,刷新,跳转,关键字,描述,IE兼容
            <meta http-equiv="X-UA-Compatible" content="IE=IE9;IE=IE8;"/>
        - title标签
        - <link />标签 icon
        - <style />标签
        - <script>标签
    7、body标签
        - 特殊字符: &nbsp; &gt; &lt;
        - p标签,表示段落,段落之间默认有间距
        - br标签,表示换行
        - h标签,表示不同大小的标题, <h1></h1> <h6></h6>
        ===== 小总结 =======
        所有标签分为:
            块级标签: h系列(加大加粗), p标签(段落和段落之间有间距)
            行内标签: span标签(白板)

刷新

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Refresh" Content="3">
    <title>老男孩</title>
</head>
<body>
<a href="https://www.oldboyedu.com">老男孩</a>
</body>
</html>

跳转

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Refresh" Content="3;Url=https://www.autohome.com.cn">
    <title>老男孩</title>
</head>
<body>
<a href="https://www.oldboyedu.com">老男孩</a>
</body>
</html>

link icon

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Refresh" Content="3">
    <title>老男孩</title>
    <link rel="shortcut icon" href="456.PNG">
</head>
<body>
<a href="https://www.oldboyedu.com">老男孩</a>
</body>
</html>

特殊字符,空格、大于号、小于号

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <a href="https://www.oldboyedu.com">a&nbsp;&nbsp;&lt;a&gt;b</a>
</body>
</html>
原文地址:https://www.cnblogs.com/python-abc/p/11717109.html