2022年 11月 3日

python实现一个简单的http服务器

需求:用python实现一个简单的http服务器

网页源码文件:https://download.csdn.net/download/d1240673769/46963534

该文件为html文件,如下图:
在这里插入图片描述
用python搭建一个静态的http服务器,通过web访问 python 目录下的文件内容,如下图
在这里插入图片描述

系统环境:centos7、python3

将网页源码文件包下载后,上传到centos中 /web/路径下,如下图:
在这里插入图片描述

实现代码:

# -*- coding: utf-8 -*-

import socket
import re
import urllib.parse

def service_client(new_socket):
    # 为这个客户端返回数据
    # 1.接收浏览器发过来的请求,即http请求
    # GET / HTTP/1.1
    request = new_socket.recv(1024).decode('utf-8')
    request_header_lines = request.splitlines()
    ret = re.match(r'[^/]+(/[^ ]*)',request_header_lines[0])
    path_name = "/"
    if ret:
        path = ret.group(1) # 取出请求中的路径名
        path_name = urllib.parse.unquote(path) # 浏览器请求的路径中带有中文,会被自动编码,需要先解码成中文,才能找到后台中对应的html文件
        print("请求路径:{}".format(path_name))

    if path_name == "/":  # 用户请求/时,返回index.html页面
        path_name = "/index.html"
 
    # 2.返回http格式的数据给浏览器
    file_name = '/web/python' + path_name
    try:
        f = open(file_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()
        # 准备发给浏览器的数据 -- header
        response = "HTTP/1.1 200 OK\r\n"
        response += "\r\n"
        new_socket.send(response.encode("utf-8"))
        new_socket.send(html_content)    
    # 关闭套接字
    new_socket.close()


def main():
    # 用来完成整体的控制
    # 1.创建套接字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 设置当服务器先close 即服务器端4次挥手之后资源能够立即释放,这样就保证了,下次运行程序时 可以立即绑定7788端口
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # 2.绑定
    tcp_server_socket.bind(("",8100))    
    # 3.变为监听套接字
    tcp_server_socket.listen(128)
    
    while True:
        # 4.等待新客户端的链接
        new_socket, client_addr = tcp_server_socket.accept()       
        # 5.为这个客户端服务
        service_client(new_socket)
        
    # 关闭监听套接字
    tcp_server_socket.close()
        
if __name__ == '__main__':
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

运行程序后,通过浏览器访问:http://192.168.149.150:8100

使用多进程实现

# -*- coding: utf-8 -*-

import socket
import re
import urllib.parse
import multiprocessing

def service_client(new_socket):
    # 为这个客户端返回数据
    # 1.接收浏览器发过来的请求,即http请求
    # GET / HTTP/1.1
    request = new_socket.recv(1024).decode('utf-8')
    request_header_lines = request.splitlines()
    ret = re.match(r'[^/]+(/[^ ]*)',request_header_lines[0])
    path_name = "/"
    if ret:
        path = ret.group(1) # 取出请求中的路径名
        path_name = urllib.parse.unquote(path) # 浏览器请求的路径中带有中文,会被自动编码,需要先解码成中文,才能找到后台中对应的html文件
        print("请求路径:{}".format(path_name))

    if path_name == "/":  # 用户请求/时,返回index.html页面
        path_name = "/index.html"
 
    # 2.返回http格式的数据给浏览器
    file_name = '/web/python' + path_name
    try:
        f = open(file_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()
        # 准备发给浏览器的数据 -- header
        response = "HTTP/1.1 200 OK\r\n"
        response += "\r\n"
        new_socket.send(response.encode("utf-8"))
        new_socket.send(html_content)
    
    # 关闭套接字
    new_socket.close()


def main():
    # 用来完成整体的控制
    # 1.创建套接字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 设置当服务器先close 即服务器端4次挥手之后资源能够立即释放,这样就保证了,下次运行程序时 可以立即绑定7788端口
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # 2.绑定
    tcp_server_socket.bind(("",8200))
    
    # 3.变为监听套接字
    tcp_server_socket.listen(128)
    
    while True:
        # 4.等待新客户端的链接
        new_socket, client_addr = tcp_server_socket.accept()
        
        # 5.为这个客户端服务
        p = multiprocessing.Process(target=service_client, args=(new_socket,))
        p.start()
        
        new_socket.close()
        
    # 关闭监听套接字
    tcp_server_socket.close()
         
if __name__ == '__main__':
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

使用多线程实现

# -*- coding: utf-8 -*-

import socket
import re
import urllib.parse
import threading

def service_client(new_socket):
    # 为这个客户端返回数据
    # 1.接收浏览器发过来的请求,即http请求
    # GET / HTTP/1.1
    request = new_socket.recv(1024).decode('utf-8')
    request_header_lines = request.splitlines()
    ret = re.match(r'[^/]+(/[^ ]*)',request_header_lines[0])
    path_name = "/"
    if ret:
        path = ret.group(1) # 取出请求中的路径名
        path_name = urllib.parse.unquote(path) # 浏览器请求的路径中带有中文,会被自动编码,需要先解码成中文,才能找到后台中对应的html文件
        print("请求路径:{}".format(path_name))

    if path_name == "/":  # 用户请求/时,返回index.html页面
        path_name = "/index.html"
 
    # 2.返回http格式的数据给浏览器
    file_name = '/web/python' + path_name
    try:
        f = open(file_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()
        # 准备发给浏览器的数据 -- header
        response = "HTTP/1.1 200 OK\r\n"
        response += "\r\n"
        new_socket.send(response.encode("utf-8"))
        new_socket.send(html_content)
    
    # 关闭套接字
    new_socket.close()


def main():
    # 用来完成整体的控制
    # 1.创建套接字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 设置当服务器先close 即服务器端4次挥手之后资源能够立即释放,这样就保证了,下次运行程序时 可以立即绑定7788端口
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # 2.绑定
    tcp_server_socket.bind(("",8200))
    
    # 3.变为监听套接字
    tcp_server_socket.listen(128)
    
    while True:
        # 4.等待新客户端的链接
        new_socket, client_addr = tcp_server_socket.accept()
        
        # 5.为这个客户端服务
        t = threading.Thread(target=service_client, args=(new_socket,))
        t.start()
        
    # 关闭监听套接字
    tcp_server_socket.close()
         

if __name__ == '__main__':
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

使用gevent实现

# -*- coding: utf-8 -*-

import socket
import re
import urllib.parse
import gevent
from gevent import monkey

monkey.patch_all()

def service_client(new_socket):
    # 为这个客户端返回数据
    # 1.接收浏览器发过来的请求,即http请求
    # GET / HTTP/1.1
    request = new_socket.recv(1024).decode('utf-8')
    request_header_lines = request.splitlines()
    ret = re.match(r'[^/]+(/[^ ]*)',request_header_lines[0])
    path_name = "/"
    if ret:
        path = ret.group(1) # 取出请求中的路径名
        path_name = urllib.parse.unquote(path) # 浏览器请求的路径中带有中文,会被自动编码,需要先解码成中文,才能找到后台中对应的html文件
        print("请求路径:{}".format(path_name))

    if path_name == "/":  # 用户请求/时,返回index.html页面
        path_name = "/index.html"
 
    # 2.返回http格式的数据给浏览器
    file_name = '/web/python' + path_name
    try:
        f = open(file_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()
        # 准备发给浏览器的数据 -- header
        response = "HTTP/1.1 200 OK\r\n"
        response += "\r\n"
        new_socket.send(response.encode("utf-8"))
        new_socket.send(html_content)
    
    # 关闭套接字
    new_socket.close()


def main():
    # 用来完成整体的控制
    # 1.创建套接字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 设置当服务器先close 即服务器端4次挥手之后资源能够立即释放,这样就保证了,下次运行程序时 可以立即绑定7788端口
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # 2.绑定
    tcp_server_socket.bind(("",8100))
    
    # 3.变为监听套接字
    tcp_server_socket.listen(128)
    
    while True:
        # 4.等待新客户端的链接
        new_socket, client_addr = tcp_server_socket.accept()
        
        # 5.为这个客户端服务
        gevent.spawn(service_client, new_socket)
        
    # 关闭监听套接字
    tcp_server_socket.close()
         
if __name__ == '__main__':
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

单进程、单线程、长链接实现多任务

# -*- coding: utf-8 -*-

import socket
import re
import urllib.parse

def service_client(new_socket, request):
    # 为这个客户端返回数据
    # 1.接收浏览器发过来的请求,即http请求
    # GET / HTTP/1.1
    request_header_lines = request.splitlines()
    ret = re.match(r'[^/]+(/[^ ]*)',request_header_lines[0])
    path_name = "/"
    if ret:
        path = ret.group(1) # 取出请求中的路径名
        path_name = urllib.parse.unquote(path) # 浏览器请求的路径中带有中文,会被自动编码,需要先解码成中文,才能找到后台中对应的html文件
        print("请求路径:{}".format(path_name))

    if path_name == "/":  # 用户请求/时,返回index.html页面
        path_name = "/index.html"
 
    # 2.返回http格式的数据给浏览器
    file_name = '/web/python' + path_name
    try:
        f = open(file_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:
        response_body = f.read()
        f.close()
        # 准备发给浏览器的数据 -- header
        response_header = "HTTP/1.1 200 OK\r\n"
        response_header += "Content-Length:{}\r\n".format(len(response_body)) # 这个字段表示一条数据的长度
        response_header += "\r\n"
        new_socket.send(response_header.encode("utf-8"))
        new_socket.send(response_body)


def main():
    # 用来完成整体的控制
    # 1.创建套接字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 设置当服务器先close 即服务器端4次挥手之后资源能够立即释放,这样就保证了,下次运行程序时 可以立即绑定7788端口
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # 2.绑定
    tcp_server_socket.bind(("",8100))
    
    # 3.变为监听套接字
    tcp_server_socket.listen(128)
    
    tcp_server_socket.setblocking(False) # 设置套接字为非堵塞的方式
    
    client_socket_list = list() # 创建一个列表,目的是将已将连接的客户端加入到列表中
    
    while True:
        # 4.等待新客户端的链接
        try:
            new_socket, client_addr = tcp_server_socket.accept()
        except Exception as ret:
            pass
        else:
            new_socket.setblocking(False)
            client_socket_list.append(new_socket)
            
        for client_socket in client_socket_list:
            try:
                recv_data = client_socket.recv(1024).decode("utf-8")
            except Exception as ret:
                pass
            else:
                if recv_data:
                    service_client(client_socket,recv_data)
                else:
                    client_socket.close()
                    client_socket_list.remove(client_socket)
        
    # 关闭监听套接字
    tcp_server_socket.close()
         

if __name__ == '__main__':
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87