Chỉ mục máy chủ cục bộ html

Nó lưu trữ các

DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Titletitle>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/main.css">
head>
<body>
    <h1>Sun* Cyber Security Researchh1>
body>
html>
0 hoặc các
DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Titletitle>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/main.css">
head>
<body>
    <h1>Sun* Cyber Security Researchh1>
body>
html>
1 và sử dụng giao thức
DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Titletitle>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/main.css">
head>
<body>
    <h1>Sun* Cyber Security Researchh1>
body>
html>
2 để gửi các tệp đến trình duyệt web

  • Một số máy chủ web nổi tiếng như. Máy chủ web Nginx, máy chủ web Apache, máy chủ web IIS, máy chủ web Tốc độ ánh sáng

  • Cách thức máy chủ web hoặc động

    Chỉ mục máy chủ cục bộ html

    Vũ Tiến Hòa @vu. tiền. hoa

    Theo dõi

    595 22 16

    Đã đăng vào ngày 4 tháng 12 năm 2020 9. 21 SA

    trong

    Nhóm An ninh mạng Sun*

    Hãy hack tất cả mọi thứ

    Chỉ mục máy chủ cục bộ html
    Chỉ mục máy chủ cục bộ html

    Theo dõi bởi 728 người.

    Theo dõi

    Sun* Cyber Security Team

    5 phút đọc

    11. 7k

    0

    5

    Hướng dẫn tạo HTTP Server bằng python

    • Report
    • Add to series of me

    Bài đăng này đã không được cập nhật trong 2 năm

    Giới thiệu

    Web Server là gì?

    tổng quan

    • Máy chủ web là một máy tính lưu trữ những nội dung web

    • Một máy chủ web được sử dụng để phục vụ các trang web trên mạng internet hoặc mạng nội bộ

    • Nó lưu trữ các

      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      0 hoặc các
      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      1 và sử dụng giao thức
      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      2 để gửi các tệp đến trình duyệt web

    • Một số máy chủ web nổi tiếng như. Máy chủ web Nginx, máy chủ web Apache, máy chủ web IIS, máy chủ web Tốc độ ánh sáng

    Cách thức máy chủ web hoặc động

    • Ví dụ người dùng muốn xem một trang web như

      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      3, người dùng nhập url vào trình duyệt web với điều kiện người dùng cần kết nối Internet. Khi đó bộ giao thức
      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      4 được sử dụng để thiết lập kết nối

    • Khi kết nối được thiết lập, máy khách sẽ gửi yêu cầu thông tin qua

      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      2 và chờ phản hồi từ máy chủ. Phía bên kia máy chủ nhận được yêu cầu, xử lý yêu cầu, gửi lại phản hồi cho máy khách

    Giao thức HTTP

    • Giao thức

      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      2 là viết tắt của
      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      7

    • Nó là một giao thức ở tầng ứng dụng cho phép các ứng dụng web giao tiếp và trao đổi dữ liệu

    • Nó là một giao thức dựa trên

      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      4

    • Nó được sử dụng để cung cấp nội dung.

      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      9

    • Sử dụng

      DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Titletitle>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="/main.css">
      head>
      <body>
          <h1>Sun* Cyber Security Researchh1>
      body>
      html>
      
      2 là cách thuận tiện nhất để chuyển dữ liệu nhanh chóng và đáng tin cậy trên web

    Ví dụ về thông điệp HTTP

    Cách tạo HTTP Server bằng python

    Python has an số thư viện được tích hợp để tạo một

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h1{
        color: white;
        padding: 20px 40px;
        margin: 0 auto;
        background-color: rgba(255, 255, 255, .7);
        display: inline-block;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
        border-radius: 4px;
        border-bottom: 5px solid rgba(150, 150, 150, 1);
        font-weight: 200;
        font-family: 'IBM Plex Sans Condensed', sans-serif;
    }
    
    1 dễ dàng hơn. Ví dụ bạn có thể tạo một
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h1{
        color: white;
        padding: 20px 40px;
        margin: 0 auto;
        background-color: rgba(255, 255, 255, .7);
        display: inline-block;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
        border-radius: 4px;
        border-bottom: 5px solid rgba(150, 150, 150, 1);
        font-weight: 200;
        font-family: 'IBM Plex Sans Condensed', sans-serif;
    }
    
    2 với một lệnh đơn giản

    • Với python2
    python -m SimpleHTTPServer + port(nếu không viết gì mặc định là 8000)
    
    • Với python3
    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    

    Nhưng bạn không thể tùy chỉnh máy chủ của mình

    Tạo một dự án HTTPServer

    Tạo tệp HTML và CSS

    DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Titletitle>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="/main.css">
    head>
    <body>
        <h1>Sun* Cyber Security Researchh1>
    body>
    html>
    
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h1{
        color: white;
        padding: 20px 40px;
        margin: 0 auto;
        background-color: rgba(255, 255, 255, .7);
        display: inline-block;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
        border-radius: 4px;
        border-bottom: 5px solid rgba(150, 150, 150, 1);
        font-weight: 200;
        font-family: 'IBM Plex Sans Condensed', sans-serif;
    }
    

    Thiết lập dự án

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h1{
        color: white;
        padding: 20px 40px;
        margin: 0 auto;
        background-color: rgba(255, 255, 255, .7);
        display: inline-block;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
        border-radius: 4px;
        border-bottom: 5px solid rgba(150, 150, 150, 1);
        font-weight: 200;
        font-family: 'IBM Plex Sans Condensed', sans-serif;
    }
    
    3

    from http.server import BaseHTTPRequestHandler
    import os
    class Server(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path == '/':
                self.path = '/index.html'
            try:
                split_path = os.path.splitext(self.path)
                request_extension = split_path[1]
                if request_extension != ".py":
                    f = open(self.path[1:]).read()
                    self.send_response(200)
                    self.end_headers()
                    self.wfile.write(bytes(f, 'utf-8'))
                else:
                    f = "File not found"
                    self.send_error(404,f)
            except:
                f = "File not found"
                self.send_error(404,f)
    
    from http.server import BaseHTTPRequestHandler
    import os
    
    • BaseHTTPRequestHandler được sử dụng để xử lý các yêu cầu HTTP đến máy chủ

    • Ngoài ra

      @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
      
      body{
          background-color: #222;
      }
      
      h1{
          color: white;
          padding: 20px 40px;
          margin: 0 auto;
          background-color: rgba(255, 255, 255, .7);
          display: inline-block;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translateY(-50%) translateX(-50%);
          border-radius: 4px;
          border-bottom: 5px solid rgba(150, 150, 150, 1);
          font-weight: 200;
          font-family: 'IBM Plex Sans Condensed', sans-serif;
      }
      
      4 còn hỗ trợ một số thuộc tính và phương thức sau

      • @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
        
        body{
            background-color: #222;
        }
        
        h1{
            color: white;
            padding: 20px 40px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, .7);
            display: inline-block;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translateY(-50%) translateX(-50%);
            border-radius: 4px;
            border-bottom: 5px solid rgba(150, 150, 150, 1);
            font-weight: 200;
            font-family: 'IBM Plex Sans Condensed', sans-serif;
        }
        
        5. Phương thức này xử lý khi có yêu cầu GET gửi lên
      • @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
        
        body{
            background-color: #222;
        }
        
        h1{
            color: white;
            padding: 20px 40px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, .7);
            display: inline-block;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translateY(-50%) translateX(-50%);
            border-radius: 4px;
            border-bottom: 5px solid rgba(150, 150, 150, 1);
            font-weight: 200;
            font-family: 'IBM Plex Sans Condensed', sans-serif;
        }
        
        6. Phương thức này xử lý khi có yêu cầu POST gửi lên
      • @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
        
        body{
            background-color: #222;
        }
        
        h1{
            color: white;
            padding: 20px 40px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, .7);
            display: inline-block;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translateY(-50%) translateX(-50%);
            border-radius: 4px;
            border-bottom: 5px solid rgba(150, 150, 150, 1);
            font-weight: 200;
            font-family: 'IBM Plex Sans Condensed', sans-serif;
        }
        
        7. Thuộc tính này trả về đường dẫn của yêu cầu
      • @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
        
        body{
            background-color: #222;
        }
        
        h1{
            color: white;
            padding: 20px 40px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, .7);
            display: inline-block;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translateY(-50%) translateX(-50%);
            border-radius: 4px;
            border-bottom: 5px solid rgba(150, 150, 150, 1);
            font-weight: 200;
            font-family: 'IBM Plex Sans Condensed', sans-serif;
        }
        
        8. Phương thức này trả về lỗi HTTP cho khách hàng

    Đầu tiên định nghĩa một phương thứ

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h1{
        color: white;
        padding: 20px 40px;
        margin: 0 auto;
        background-color: rgba(255, 255, 255, .7);
        display: inline-block;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
        border-radius: 4px;
        border-bottom: 5px solid rgba(150, 150, 150, 1);
        font-weight: 200;
        font-family: 'IBM Plex Sans Condensed', sans-serif;
    }
    
    5. Phương thức này chạy khi có yêu cầu GET gửi lên

    • bản thân. đường dẫn =='/' kiểm tra xem yêu cầu gửi lên có phải trang chỉ mục hay không và nếu là trang

      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      0 thì gán đường dẫn cho chính chỉ mục. đường dẫn == '/chỉ mục. html`

    • Tiếp tục cố gắng đọc các tệp mà người dùng đang cố gắng truy cập ngoại trừ những

      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      1 tránh làm lộ
      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      2

    • Nếu tệp yêu cầu được tìm thấy, máy chủ sẽ gửi phản hồi

      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      3.
      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      3 bị phản hồi mà bất cứ khi nào bạn truy cập thành công một trang web

    • Nếu yêu cầu tập tin không được tìm thấy, thì

      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      5 gửi mã lỗi tập tin yêu cầu không hợp lệ

    • Sử dụng phương thức mã hóa

      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      6
      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      7 để chuyển đổi định dạng văn bản sang byte

    from http.server import BaseHTTPRequestHandler
    import os
    class Server(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path == '/':
                self.path = '/index.html'
            try:
                split_path = os.path.splitext(self.path)
                request_extension = split_path[1]
                if request_extension != ".py":
                    f = open(self.path[1:]).read()
                    self.send_response(200)
                    self.end_headers()
                    self.wfile.write(bytes(f, 'utf-8'))
                else:
                    f = "File not found"
                    self.send_error(404,f)
            except:
                f = "File not found"
                self.send_error(404,f)
    
    8

    import time
    from http.server import HTTPServer
    from server import Server
    
    HOST_NAME = 'localhost'
    PORT = 8000
    
    if __name__ == "__main__":
        httpd = HTTPServer((HOST_NAME,PORT),Server)
        print(time.asctime(), "Start Server - %s:%s"%(HOST_NAME,PORT))
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        httpd.server_close()
        print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))
    
    import time
    from http.server import HTTPServer
    from server import Server
    
    • sử dụng thời gian để kiếm soát thời gian máy chủ

      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      9 và máy chủ
      from http.server import BaseHTTPRequestHandler
      import os
      
      0

    • HTTPServer là một lớp con socketserver. TCPServer nó được tạo và lắng nghe ổ cắm HTTP, gửi yêu cầu đến bộ xử lý

    Cấu hình máy chủ với 2 hằng số

    from http.server import BaseHTTPRequestHandler
    import os
    
    1 với
    from http.server import BaseHTTPRequestHandler
    import os
    
    2 dùng để chạy máy chủ trên localhost và
    from http.server import BaseHTTPRequestHandler
    import os
    
    3 là cổng để chạy ứng dụng

    from http.server import BaseHTTPRequestHandler
    import os
    
    4

    • Gọi HTTPServer mà python cung cấp với đối số thứ nhất là một cặp
      from http.server import BaseHTTPRequestHandler
      import os
      
      5 và đối số thứ 2 là một lớp
      from http.server import BaseHTTPRequestHandler
      import os
      
      6 để xử lý đã được thiết lập trước đó
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))
    
    • Khối tiếp theo đến máy chủ
      from http.server import BaseHTTPRequestHandler
      import os
      class Server(BaseHTTPRequestHandler):
          def do_GET(self):
              if self.path == '/':
                  self.path = '/index.html'
              try:
                  split_path = os.path.splitext(self.path)
                  request_extension = split_path[1]
                  if request_extension != ".py":
                      f = open(self.path[1:]).read()
                      self.send_response(200)
                      self.end_headers()
                      self.wfile.write(bytes(f, 'utf-8'))
                  else:
                      f = "File not found"
                      self.send_error(404,f)
              except:
                  f = "File not found"
                  self.send_error(404,f)
      
      9 và máy chủ
      from http.server import BaseHTTPRequestHandler
      import os
      
      0, khi nhận được tín hiệu ngắt từ bàn phím máy chủ sẽ đóng kết nối với máy chủ
      from http.server import BaseHTTPRequestHandler
      import os
      
      9

    Bây giờ chúng ta khởi động máy chủ bằng cách chạy lệnh

    python3 main.py
    
    • Open the input url http. //máy chủ cục bộ. 8000

    • Và đây là phản hồi từ phía máy chủ.

    Vì vậy, chúng tôi đã tạo thành công được một Máy chủ HTTP đơn giản với python, ngoài ra chúng tôi còn có thể viết thêm các phương thức như

    import time
    from http.server import HTTPServer
    from server import Server
    
    HOST_NAME = 'localhost'
    PORT = 8000
    
    if __name__ == "__main__":
        httpd = HTTPServer((HOST_NAME,PORT),Server)
        print(time.asctime(), "Start Server - %s:%s"%(HOST_NAME,PORT))
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        httpd.server_close()
        print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))
    
    0 để Server có thể xử lý nhiều phương thức hơn