WEB框架原理(socket)

socket服务端 

socket示例
import socket
sk = socket.socket()
sk.bind(('127.0.0.1', 8000))
sk.listen()

while 1:
    conn, addr = sk.accept()
    ret = conn.recv(10240)
    print(ret)
    conn.send(b"HTTP/1.1 200 OK\r\n\r\nooooojiba98k")
不同路劲返回不同的内容
import socket
sk = socket.socket()
sk.bind(('127.0.0.1', 8000))
sk.listen()

while 1:
    conn, addr = sk.accept()
    ret = conn.recv(10240)
    ret_str = str(ret, encoding='utf-8')
    res = ret_str.split('\r\n')[0].split(' ')[1]
    print(res)
    if res == '/aaa/':
        msg = b'123'
    elif res == '/bbb/':
        msg = b'456'
    else:
        msg = b'789'
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    conn.send(msg)
    conn.close()
不同路劲返回不同内容(函数版)
import socket
sk = socket.socket()
sk.bind(('127.0.0.1', 8000))
sk.listen()


def aaa(res):
    data = 'What you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


def bbb(res):
    data = 'What you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


while 1:
    conn,addr = sk.accept()
    ret = conn.recv(10240)
    ret_str = str(ret, encoding='utf-8')
    res = ret_str.split('\r\n')[0].split(' ')[1]
    print(res)
    if res == '/aaa/':
        msg = aaa(res)
    elif res == '/bbb/':
        msg = bbb(res)
    else:
        msg = b'Not found'
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    conn.send(msg)
    conn.close()
不同路径返回不同内容(反射版)
import socket
sk = socket.socket()
sk.bind(('127.0.0.1', 8000))
sk.listen()


def aaa(res):
    data = 'what you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


def bbb(res):
    data = 'what you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


res_list = [
    ('/aaa/', aaa),
    ('/bbb/', bbb)
]
while 1:
    # ------连接部分------
    conn,addr = sk.accept()
    ret = conn.recv(10240)
    # 收到的字节转化成字符串
    ret_str = str(ret, encoding='utf-8')
    # 对请求行进行切割
    res = ret_str.split('\r\n')[0].split(' ')[1]
    # -------业务处理部分--------
    # 使用func变量保存将要执行的函数
    for i in res_list:
        if i[0] == res:
            func = i[1]
            break
    else:
        func = None
    # 执行函数
    if func:
        msg = func(res)
    else:
        msg = b'404Not found'
    # ----回复响应消息----
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    conn.send(msg)
    conn.close()
返回网页
import socket
sk = socket.socket()
sk.bind(('127.0.0.1', 8000))
sk.listen()


def aaa(res):
    data = 'what you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


def bbb(res):
    data = 'what you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


def login(res):
    with open('login.html', 'rb')as f:
        data = f.read()
    return data


res_list = [
    ('/aaa/', aaa),
    ('/bbb/', bbb),
    ('/login/', login),
]
while 1:
    # ------连接部分------
    conn,addr = sk.accept()
    ret = conn.recv(10240)
    # 收到的字节转化成字符串
    ret_str = str(ret, encoding='utf-8')
    # 对请求行进行切割
    res = ret_str.split('\r\n')[0].split(' ')[1]
    # -------业务处理部分--------
    # 使用func变量保存将要执行的函数
    for i in res_list:
        if i[0] == res:
            func = i[1]
            break
    else:
        func = None
    # 执行函数
    if func:
        msg = func(res)
    else:
        msg = b'404Not found'
    # ----回复响应消息----
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    conn.send(msg)
    conn.close()
返回动态网页
import socket
import time
sk = socket.socket()
sk.bind(('127.0.0.1', 8800))
sk.listen()


def aaa(res):
    data = 'what you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


def bbb(res):
    data = 'what you visited was:{}'.format(res)
    return bytes(data, encoding='utf-8')


def login(res):
    with open('login.html', 'r', encoding='utf-8')as f:
        data = f.read()
    now = str(time.time())
    data = data.replace('@@**@@', now)
    return bytes(data, encoding='utf-8')


res_list = [
    ('/aaa/', aaa),
    ('/bbb/', bbb),
    ('/login/', login),
]
while 1:
    # ------连接部分------
    conn, addr = sk.accept()
    ret = conn.recv(10240)
    # 收到的字节转化成字符串
    ret_str = str(ret, encoding='utf-8')
    # 对请求行进行切割
    res = ret_str.split('\r\n')[0].split(' ')[1]
    # -------业务处理部分--------
    # 使用func变量保存将要执行的函数
    for i in res_list:
        if i[0] == res:
            func = i[1]
            break
    else:
        func = None
    # 执行函数
    if func:
        msg = func(res)
    else:
        msg = b'404Not found'
    # ----回复响应消息----
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    conn.send(msg)
    conn.close()

 

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<h1>This is login page!</h1>
<p>时间:@@**@@</p>
</body>
</html>
HTML文件

 

转载于:https://www.cnblogs.com/chenyibai/p/9761853.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值