tcp/ip学习之18: HTTP协议和解析

一、 HTTP协议服务器的实现
本例程用来下载文件

from http.server import HTTPServer, BaseHTTPRequestHandler
import os
import logging

# 配置日志
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

class SimpleRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        """Handle GET requests"""
        try:
            # Print client request details for debugging
            logging.debug(f"Client IP: {self.client_address[0]}")
            logging.debug(f"Request line: {self.requestline}")
            logging.debug(f"Headers: {self.headers}")

            # Get the file path from the URL
            file_path = self.path.lstrip('/')
            if not file_path:
                self.send_response(404)
                self.end_headers()
                self.wfile.write(b"File not found")
                return

            # Check if the file exists
            if not os.path.exists(file_path):
                self.send_response(404)
                self.end_headers()
                self.wfile.write(b"File not found")
                return

            # Get the file size
            file_size = os.path.getsize(file_path)

            # Send the full file
            self.send_response(200)
            self.send_header('Content-Type', 'application/octet-stream')
            self.send_header('Content-Length', str(file_size))  # Add Content-Length header
            self.end_headers()

            # Send the file in chunks
            chunk_size = 1024  # 1KB chunks
            with open(file_path, 'rb') as file:
                while True:
                    chunk = file.read(chunk_size)
                    if not chunk:
                        break
                    self.wfile.write(chunk)

        except ConnectionResetError as e:
            logging.error(f"Connection reset by peer: {e}")
            logging.error(f"Client IP: {self.client_address[0]}")
            logging.error(f"Request: {self.requestline}")
            logging.error(f"File path: {file_path}")
            logging.error(f"File size: {file_size}")
        except Exception as e:
            logging.error(f"An error occurred: {e}")
            logging.error(f"Client IP: {self.client_address[0]}")
            logging.error(f"Request: {self.requestline}")
            logging.error(f"File path: {file_path}")
            logging.error(f"File size: {file_size}")

def run(server_class=HTTPServer, handler_class=SimpleRequestHandler, port=80):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    logging.info(f"Starting httpd server on port {port}...")
    httpd.serve_forever()

if __name__ == '__main__':
    run()

运行效果:

2025-09-12 10:22:58,760 - INFO - Starting httpd server on port 80...
2025-09-12 10:23:17,549 - DEBUG - Client IP: 192.168.1.2
2025-09-12 10:23:17,549 - DEBUG - Request line: GET /application.bin HTTP/1.1
2025-09-12 10:23:17,550 - DEBUG - Headers: Host: 192.168.1.104
。。。

二、抓包
image

三、分析
1、请求

0000   00 e0 4c 68 03 8d 02 00 00 30 00 4a 08 00 45 00
0010   00 5e 00 0a 00 00 ff 06 37 d5 c0 a8 01 02 c0 a8
0020   01 68 c0 02 00 50 00 00 19 6e c7 f4 d4 b4 50 18
0030   0b 68 a9 ea 00 00 47 45 54 20 2f 61 70 70 6c 69
0040   63 61 74 69 6f 6e 2e 62 69 6e 20 48 54 54 50 2f
0050   31 2e 31 0d 0a 48 6f 73 74 3a 20 31 39 32 2e 31
0060   36 38 2e 31 2e 31 30 34 0d 0a 0d 0a

2、回复

0000   02 00 00 30 00 4a 00 e0 4c 68 03 8d 08 00 45 00
0010   03 cc 3c cd 40 00 80 06 00 00 c0 a8 01 68 c0 a8
0020   01 02 00 50 c0 02 c7 f6 14 ae 00 00 19 a4 50 19
0030   ff c9 87 79 00 00 a2 3b 01 08 aa 3b 01 08 35 31
0040   01 08 cd 3b 01 08 db 3b 01 08 35 31 01 08 fe 3b
0050   01 08 0a 3c 01 08 45 9b 00 08 16 3c 01 08 28 3c
0060   01 08 45 9b 00 08 34 3c 01 08 3d 3c 01 08 0d 9a
0070   00 08 56 3c 01 08 65 3c 01 08 0d 9a 00 08 7e 3c
0080   01 08 8b 3c 01 08 f5 96 00 08 a3 3c 01 08 b6 3c
0090   01 08 f5 96 00 08 ce 3c 01 08 dc 3c 01 08 c9 98
00a0   00 08 f9 3c 01 08 0d 3d 01 08 c9 98 00 08 2a 3d
00b0   01 08 35 3d 01 08 29 9d 00 08 4a 3d 01 08 5b 3d
00c0   01 08 29 9d 00 08 70 3d 01 08 75 3d 01 08 d9 95
00d0   00 08 8f 3d 01 08 9a 3d 01 08 4d b0 00 08 b0 3d
00e0   01 08 b9 3d 01 08 4b 60 00 08 d5 3d 01 08 e0 3d
00f0   01 08 3d 60 00 08 05 3e 01 08 0e 3e 01 08 51 98
0100   00 08 6c 40 01 08 00 00 00 20 6c 04 00 00 d8 0f
0110   00 08 94 41 01 08 00 00 00 10 a8 10 00 00 d8 0f
0120   00 08 94 41 01 08 6c 04 00 20 84 61 01 00 58 4a
0130   00 08 04 43 01 08 a8 10 00 10 40 55 00 00 58 4a
0140   00 08 01 24 73 7f ff 14 08 02 40 22 02 13 01 03
0150   43 01 01 1a 04 10 19 09 79 10 2c 20 02 40 08 a9
0160   10 da 10 10 da 20 10 da 40 10 aa 80 10 da 0c 10
0170   e9 70 59 14 19 93 29 14 29 07 39 10 59 84 49 10
0180   8a 18 64 69 10 69 54 69 10 69 54 79 10 59 e4 79
0190   10 19 c5 79 10 19 a4 e9 f4 69 30 79 10 da 04 10
01a0   9a 08 10 29 50 19 81 89 c4 1b 02 40 5f 89 b0 19
01b0   10 da 20 10 59 c0 79 10 59 80 89 10 49 80 89 10
01c0   49 80 89 10 49 80 49 10 2c 08 02 40 78 a9 10 19
01d0   2f b9 10 19 2f b9 10 b9 70 29 10 a9 70 da 0c a0
01e0   19 10 b9 40 7a 04 f0 89 10 19 31 79 10 1c 14 02
01f0   40 1f 29 10 29 97 39 10 59 70 89 10 49 70 89 10
0200   49 e0 49 10 7a 10 c0 79 10 59 80 79 10 da 10 10
0210   da 20 10 59 c0 79 60 e9 10 09 2f a0 19 81 79 10
0220   da 1c a0 19 10 b9 a0 19 10 b9 40 29 10 a9 e0 29
0230   10 d9 50 11 19 d1 b9 10 19 d1 b9 10 19 c1 b9 10
0240   19 c1 79 10 12 c0 12 c4 12 c8 12 cc 12 d0 12 d4
0250   12 d8 02 2c dc 1e 54 37 01 08 6c 04 1a 64 04 1a
0260   74 04 1a 5c 04 49 55 01 26 00 10 10 4f 4a 1f 1a
0270   01 04 21 24 31 34 41 44 11 14 05 20 31 25 20 35
0280   30 45 40 15 10 17 06 47 26 37 36 27 46 07 16 13
0290   02 43 22 33 32 23 42 03 12 09 0c 29 2c 39 3c 49
02a0   4c 19 1c 0d 08 2d 28 3d 38 4d 48 1d 18 09 3e 42
02b0   09 40 82 10 22 21 20 23 22 25 24 27 26 29 28 2b
02c0   2a 2d 2c 2f 2e 31 30 33 32 35 34 37 36 39 38 3b
02d0   3a 3d 3c 3f 3e 01 10 24 03 02 05 04 07 06 09 08
02e0   0b 0a 0d 0c 0f 0e 11 10 13 12 15 14 17 16 19 18
02f0   1b 1a 1d 1c 1f 1e 05 04 02 03 01 00 3b 2c 06 07
0300   0d 0c 0b 0a 08 09 0f 0e 15 14 11 10 12 13 17 16
0310   1d 1c 1b 1a 19 18 1e 1f 25 24 23 22 20 21 26 27
0320   2c 2d 2b 2a 29 28 2e 2f 34 35 33 32 31 30 36 37
0330   3d 3c 3a 3b 38 39 3e 3f 03 1e 55 aa 70 0a 42 54
0340   5f 56 31 2e 30 2e 30 cb 48 57 10 bc 41 50 50 11
0350   38 0b 45 54 53 43 4e 6f 2e 31 5b 30 01 82 5d 1d
0360   c0 a8 01 02 04 1a 68 04 15 01 ff ff ff 06 ff 89
0370   13 8a 13 02 01 ff 01 ff 01 ff 01 ff 01 ff 01 ff
0380   01 ff 01 ff 01 ff 01 ff 01 ff 01 ff 01 9c 33 90
0390   92 84 7a 03 0a 68 09 01 02 03 04 06 07 08 09 0c
03a0   a9 10 01 15 02 1e c0 03 5e 6e 19 32 01 13 40 10
03b0   2a 10 04 32 78 32 02 1a 50 0c 29 04 32 20 32 05
03c0   1a 60 0c 29 04 32 30 32 06 1a 70 0c 29 04 32 3c
03d0   29 f9 1a 80 0c 29 04 02 1f 2c
posted @ 2025-09-12 10:27  cupid8505  阅读(8)  评论(0)    收藏  举报