web3:多进程, 线程实现http服务器

多进程实现http服务器
多线程实现http服务器

多进程实现http服务器

导入multiprocessing模块

import multiprocessing

打开一个进程给客户端,在主进程先关闭new_socket.close()

    while True:
        # 客户端连接
        new_socket,client_addr = tcp_server_socket.accept()

        # 打开一个线程给客户端
        p = multiprocessing.Process(target=client_socket,args=(new_socket,))
        p.start()
        new_socket.close()

代码:(其他部分与上一章一样)

import socket
import multiprocessing
import re

def client_socket(new_socket):
    recv_data = new_socket.recv(1024).decode("utf-8")
    print (recv_data)
    request_lines = recv_data.splitlines()
    print(">"*20)
    print (request_lines)
    # 用正则表达式把recv_data的值提取出来
    ret = re.match(r"[^/]+(/[^ ]*)",request_lines[0])
    if ret:
        html_name = ret.group(1)
        print (html_name)

    response = "HTTP/1.1 200 OK\r\n"
    response += "\r\n"

    try:
        f = open("." + html_name,"rb")
    except:
        response = "HTTP/1.1 404 NOT found\r\n"
        response += "\r\n"
        response += "-----file not found----"
        new_socket.send(response.encode("utf-8"))
    else:
        html_content = f.read()
        f.close()
        new_socket.send(response.encode("utf-8"))
        new_socket.send(html_content)

    new_socket.close()

def main():
    # 打开套接字
    tcp_server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    # 绑定端口
    localaddr = ("",8080)
    tcp_server_socket.bind(localaddr)

    # 转为接听模式
    tcp_server_socket.listen(128)

    while True:
        # 客户端连接
        new_socket,client_addr = tcp_server_socket.accept()

        # 打开一个线程给客户端
        p = multiprocessing.Process(target=client_socket,args=(new_socket,))
        p.start()
        new_socket.close()

    # 关闭套接字
    tcp_server_socket.close()


if __name__ == '__main__':
    main()

多线程实现http服务器

导入threading模块

import threading

打开一个线程给客户端,在主线程不用关闭new_socket.close()

 while True:
        # 客户端连接
        new_socket,client_addr = tcp_server_socket.accept()

        # 打开一个线程给客户端
        p = threading.Thread(target=client_socket,args=(new_socket,))
        p.start()

    # 关闭套接字
    tcp_server_socket.close()

代码:(其他部分与上一章一样)

import socket
import threading
import re

def client_socket(new_socket):
    recv_data = new_socket.recv(1024).decode("utf-8")
    print (recv_data)
    request_lines = recv_data.splitlines()
    print(">"*20)
    print (request_lines)
    # 用正则表达式把recv_data的值提取出来
    ret = re.match(r"[^/]+(/[^ ]*)",request_lines[0])
    if ret:
        html_name = ret.group(1)
        print (html_name)

    response = "HTTP/1.1 200 OK\r\n"
    response += "\r\n"

    try:
        f = open("." + html_name,"rb")
    except:
        response = "HTTP/1.1 404 NOT found\r\n"
        response += "\r\n"
        response += "-----file not found----"
        new_socket.send(response.encode("utf-8"))
    else:
        html_content = f.read()
        f.close()
        new_socket.send(response.encode("utf-8"))
        new_socket.send(html_content)

    new_socket.close()

def main():
    # 打开套接字
    tcp_server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    # 绑定端口
    localaddr = ("",8080)
    tcp_server_socket.bind(localaddr)

    # 转为接听模式
    tcp_server_socket.listen(128)

    while True:
        # 客户端连接
        new_socket,client_addr = tcp_server_socket.accept()

        # 打开一个线程给客户端
        p = threading.Thread(target=client_socket,args=(new_socket,))
        p.start()

    # 关闭套接字
    tcp_server_socket.close()


if __name__ == '__main__':
    main()

  • 问题,为什么要在主进程也close(),在子进程也close()
    而在主线程不close()
    在这里插入图片描述

进程之间的变量互相独立,线程共享全局变量。

就是主线程关闭了,子线程当中其实也相当于关闭了,共享全局变量。
而进程之间复制一份,是各自的资源,主进程关闭了,子进程并不会关闭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值