Hướng dẫn httpx python - con trăn httpx

Giới thiệu

Web Server là gì?

Tổng quan

  • Web server là một máy tính lưu trữ những nội dung web.

  • Một máy chủ web 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

    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    0 hay các
    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    1 và sử dụng giao thức
    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    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ư : Nginx web server, Apache web server, IIS web server, Light Speed web server.Nginx web server, Apache web server, IIS web server, Light Speed web server.

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

Hướng dẫn httpx python - con trăn httpx

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

    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    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
    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    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 một yêu cầu thông qua

    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    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

    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    2 là viết tắt của
    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    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

    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    4.

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

    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    9...

  • Sử dụng

    python3 -m http.server + port(nếu không viết gì mặc định là 8000)
    
    2 là cách thuận tiện nhất để di chuyển dữ liệu nhanh chóng và đáng tin cậy trên web.

Ví dụ về HTTP message

Hướng dẫn httpx python - con trăn httpx

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

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

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>
    <h2>Sun* Cyber Security Researchh2>
body>
html>
1 dễ dàng hơn. Ví dụ bạn có thể tạo một
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>
    <h2>Sun* Cyber Security Researchh2>
body>
html>
2 với một câu 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ể custom server của bạn.

Tạo một Project HTTPServer

Hướng dẫn httpx python - con trăn httpx

Tạo file HTML and 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>
    <h2>Sun* Cyber Security Researchh2>
body>
html>
@import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');

body{
    background-color: #222;
}

h2{
    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 project

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>
    <h2>Sun* Cyber Security Researchh2>
body>
html>
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ủ. được sử dụng để xử lý các yêu cầu HTTP đến máy chủ.

  • Ngoài ra

    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>
        <h2>Sun* Cyber Security Researchh2>
    body>
    html>
    
    4 còn hỗ trợ một số thuộc tính và phương thức sau:

    • 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>
          <h2>Sun* Cyber Security Researchh2>
      body>
      html>
      
      5: Phương thức này xử lý khi có yêu cầu GET gửi lê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>
          <h2>Sun* Cyber Security Researchh2>
      body>
      html>
      
      6: Phương thức này xử lý khi có yêu cầu POST gửi lê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>
          <h2>Sun* Cyber Security Researchh2>
      body>
      html>
      
      7: Thuộc tính này trả về path của request.
    • 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>
          <h2>Sun* Cyber Security Researchh2>
      body>
      html>
      
      8: Phương thức này trả về lỗi HTTP cho client.

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

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>
    <h2>Sun* Cyber Security Researchh2>
body>
html>
5. Phương thức này chạy khi có một yêu cầu GET gửi lên.

  • self.path =='/' kiểm tra xem yêu cầu gửi lên có phải trang index hay không và nếu là trang

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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;
    }
    
    0 thì gán đường dẫn cho index self.path == '/index.html`. kiểm tra xem yêu cầu gửi lên có phải trang index hay không và nếu là trang
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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;
    }
    
    0 thì gán đường dẫn cho index self.path == '/index.html`.

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

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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 tránh làm lộ
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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.

  • Nếu tập tin yêu cầu được tìm thấy thì server gửi phản hồi

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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.
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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 là 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 tập tin yêu cầu không được tìm thấy thì

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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 gửi một mã lỗi tập tin yêu cầu không hợp lệ.

  • Sử dụng phương thức

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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 encode
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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 để chuyển đổi dạng văn bản sang bytes.

@import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');

body{
    background-color: #222;
}

h2{
    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

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
  • time sử dụng để kiếm soát thời gian

    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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;
    }
    
    9 và
    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 server.
    sử dụng để kiếm soát thời gian
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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;
    }
    
    9 và
    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 server.

  • HTTPServer là một lớp con socketserver.TCPServer nó tạo và lắng nghe HTTP socket, gửi yêu cầu đến bộ xử lý. là một lớp con socketserver.TCPServer nó tạo và lắng nghe HTTP socket, 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
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 vớ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)
2 dùng để chạy máy chủ trên localhost và
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 là cổng mà để chạy ứng dụ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)
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
    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 và đối số thứ 2 là một class
    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 để xử lý đã được thiết lập trước đó.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
    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 và đối số thứ 2 là một class
    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 để 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 để
    @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
    
    body{
        background-color: #222;
    }
    
    h2{
        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;
    }
    
    9 và
    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 server, khi nhận được tín hiệu ngắt từ bàn phím server sẽ đóng kết nối vớ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)
    
    9.

Bây giờ chúng ta start server bằng cách chạy lệnh:

python3 main.py
  • Mở trình duyệt nhập url http://localhost:8000http://localhost:8000

    Hướng dẫn httpx python - con trăn httpx

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

    Hướng dẫn httpx python - con trăn httpx

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

from http.server import BaseHTTPRequestHandler
import os
0 để cho Server có thể xử lý nhiều phương thức hơn.HTTP Server đơn giản với python, ngoài ra chúng ta còn có thể viết thêm các phương thức như
from http.server import BaseHTTPRequestHandler
import os
0 để cho Server có thể xử lý nhiều phương thức hơn.

Tài liệu

https://docs.python.org/3/library/http.server.html