首页 > 其他分享 >使用socket实现http服务端

使用socket实现http服务端

时间:2022-11-29 21:25:22浏览次数:39  
标签:http socket tcp client recv response 服务端 name

# encoding = utf-8

import re
import socket


# 接收消息的方法
def recv_msg(tcp_socket, recv_data):
    requests = recv_data.splitlines()
    print(requests)
    file_name = ""
    ret = re.match(r"[^/]+(/[^ ]*)", requests[0])
    if ret:
        file_name = ret.group(1)
        if file_name == "/":
            file_name = "templates/index.html"
    try:
        f = open('report' + file_name, 'rb')
    except:
        response = "HTTP/1.1 404 Not FOUND\r\n"
        response += "\r\n"
        response += "<h1>Not found</h1>"
        tcp_socket.send(response.encode('utf-8'))
    else:
        html_content = f.read()
        f.close()
        response_body = html_content
        response_header = "HTTP/1.1 200 OK\r\n"
        response_header += "Content-Length: %d\r\n" % len(response_body)
        response_header += "\r\n"
        response = response_header.encode('utf-8') + response_body
        tcp_socket.send(response)


# 主函数
def main():
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = "127.0.0.1"
    port = 8081
    tcp_socket.bind((host, port))
    tcp_socket.listen(128)
    tcp_socket.setblocking(False)
    client_list = []
    while True:
        try:
            new_client_socket, client_addr = tcp_socket.accept()
        except Exception as e:
            pass
        else:
            new_client_socket.setblocking(False)
            client_list.append(new_client_socket)
            print(str(client_addr))
        for client_socket in client_list:
            try:
                recv_data = client_socket.recv(1024).decode('utf-8')
            except Exception as e:
                pass
            else:
                if recv_data:
                    recv_msg(client_socket, recv_data)
                else:
                    client_list.remove(client_socket)
                    client_socket.close()


if __name__ == '__main__':
    main()

标签:http,socket,tcp,client,recv,response,服务端,name
From: https://www.cnblogs.com/saiya6/p/16936749.html

相关文章

  • Socket套接字实现TCP协议客户端
    tcp_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)host=input("请输入连接的服务的IP:")port=int(input("请输入服务的端口:"))tcp_socket.connect((h......
  • .NET6之MiniAPI(二十二):HttpClient
    说明:本篇不是说明HttpClient怎么使用,而以分享在asp.netcoreminiapi框架下,HttpClient的引入和使用方式。我们在业务开发中,免不了调用三方的服务,这时就会用到Htt......
  • .NET6之MiniAPI(二十二):HttpClient
    说明:本篇不是说明HttpClient怎么使用,而以分享在asp.netcoreminiapi框架下,HttpClient的引入和使用方式。我们在业务开发中,免不了调用三方的服务,这时就会用到Htt......
  • 如果摄像头不支持Web Socket,猿大师播放器还能在网页中播放RTSP流吗?
    问:我们的情况比较复杂,摄像头设备品牌和数量都比较多,分布在全国各地都有,地点分布比较广泛,有的甚至是比较老的型号,如果摄像头设备不支持WebSocket,猿大师播放器还可以在网页......
  • Http请求超好用的工具类
    话题不多说,直接开整1、先导入依赖<dependency> <groupId>io.github.admin4j</groupId> <artifactId>common-http-starter</artifactId> <version>0.4.2</version></d......
  • Nginx配置https并监听80端口重定向到443
    1.进入nginx安装目录,进入config文件夹编辑nginx.conf文件vimnginx.conf配置端口 443listen443http2ssldefault_server;ssl_certificate/etc/nginx/ssl/cha......
  • C# PictureBox 获取 http 网络图片
    在winform窗体项目中pictureBox控件动态赋图片this.PictureBox1.Image=Image.FromFile(@"images/AAA/A1.jpg");"images/AAA/A1.jpg"为相对目录在工程的......
  • nginx.conf 配置解析之 http配置
    官方文档http://nginx.org/en/docs/includeMIME-Type;定义nginx能识别的网络资源媒体类型(如,文本、html、js、css、流媒体等,catconf/mime.types)default_typeapplication/o......
  • tomcat-To-https
    Centos7配置Tomcat跳转Https前言:在网络安全盛行的时代下,有时业务为了安全需求要使用https协议,包括http、nginx、tomcat等,本篇简单分享一下tomcat跳转https配置。1、环......
  • Cloudfront HTTPS 性能优化
    HTTP/2相比廉颇老矣的HTTP/1.x,HTTP/2在底层传输做了很大的改动和优化包括有:每个服务器只用一个连接,节省多次建立连接的时间,在TLS上效果尤为明显加速TLS交付,HTTP/2......